我该如何做,以便使coin_toss函数中赢得的任何钱都保存在wallet变量中?
例如,如果有另一个名为dice_roll的函数,我需要它使用coin_toss之后钱包里剩下的任何东西。
import random
wallet = 100
def coin_toss(call, bet, wallet):
choices = ['heads', 'tails']
toss = random.choice(choices)
if wallet >= bet and toss == call:
wallet += bet
print('Congrats, you now have ' + str(wallet))
return wallet
elif wallet >= bet and toss != call:
wallet -= bet
print('Bummer, you now have ' + str(wallet))
return wallet
else:
print('Sorry, you do not have enough money')
coin_toss('heads',10, wallet)
print(wallet)
胜利与失败
wallet = coin_toss('heads',10, wallet)
print(wallet)