如何将用户输入添加到新变量中

时间:2018-09-20 08:49:35

标签: python

我正在为自己的代码而苦苦挣扎,似乎无法弄清楚。 因此,用户输入他们必须从多少钱开始。然后,他们输入要下注的金额。然后,如果他们赢了,输了,推动了或获得了二十一点,程序将输出。在if语句中的游戏函数中显示了获取每个概率的概率。 我想要做的是拥有他们所获得的总数并将其作为新总数,并且用户继续从该新总数进行投注,直到他们输入“ x”结束该程序为止。我不确定该怎么做。

import random
print('BLACKJACK!')
print('')
print('Blackjack payout is 3:2')
print('Enter \'x\' for bet to exit')
print('')

money = float(input('Starting money: '))
#global variables
bet_amount = 0

#play the game
def game(bet_amount):
    total = float(money)
    number = random.randint(1, 100)
    if number >= 6 and number <= 42:
        print('You won.')
        print('Money: ', total + int(bet_amount))

    elif number >= 43 and number <= 51:
        print('You pushed')
        print('Money: ', total - int(bet_amount))

    elif number >=52 and number <= 100:
        print('You lost.')
        print('Money: ', total)

    elif number >= 1 and number <= 5:
        print('You got a blackjack!')
        print('Money: ', ((int(bet_amount) * 1.5) + total))
    return total


def main():
    while True:
        print('')
        bet_amount = input('Bet amount: ')
        if bet_amount == 'x':
            print('Bye!')
            break
        else:
            game(bet_amount)


if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

您应该将新的total的计算值分配回total,而不仅仅是打印它:

import random
print('BLACKJACK!')
print('')
print('Blackjack payout is 3:2')
print('Enter \'x\' for bet to exit')
print('')

#play the game
def game(money, bet_amount):
    total = float(money)
    bet_amount = int(bet_amount)
    number = random.randint(1, 100)
    if number >= 6 and number <= 42:
        total += bet_amount
        print('You won.')

    elif number >= 43 and number <= 51:
        total -= bet_amount
        print('You pushed')

    elif number >=52 and number <= 100:
        print('You lost.')

    elif number >= 1 and number <= 5:
        total += bet_amount * 1.5
        print('You got a blackjack!')
    return total


def main():
    money = float(input('Starting money: '))
    while money >= 0:
        print('')
        bet_amount = input('Bet amount: ')
        if bet_amount == 'x':
            print('Bye!')
            break
        else:
            money = game(money, bet_amount)
            print('Money: ', money)

if __name__ == "__main__":
    main()