继续玩这个游戏,直到他们在 money_amount 中有 0 个或更少

Continue playing this game until they have 0 or less in the money_amount

我希望用户继续玩这个游戏,直到 money_amount 中的分数为 0 或更少。我怎么可能这样做?

import numpy as np

random_number = np.random.randint(1, 6)
money_amount = 10

try:
    user_wager = int(input('Your wager: '))
    if (user_wager < 0) or (user_wager > money_amount):
        print("You don't have that amount of money!")
except(ValueError):
    print('Enter a numerical value!')

try:
    user_guess = int(input('Guess: '))
    if (user_guess < 1) or (user_guess > 5):
        print('Please choose a number between 1 and 5!')
    elif user_guess == random_number:
        money_amount += user_wager
        print(f'Correct! You now have ${money_amount}!')
    else:
        money_amount -= user_wager
        print(f'Wrong! The number was {random_number} and you now have ${money_amount}.')
except(ValueError):
    print('Choose a number!')

最好的方法是使用 while 循环。来自维基:

While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is no longer met. If the condition is initially false, the loop body will not be executed at all.

对于您的游戏,您希望“当”玩家拥有“多于”“0”美元时循环。所以你的循环将是:

while money_amount > 0:
    # game logic

你可以简单地做:

while money_amount > 0:
    #game

或者这样:

while 1:
    #game
    if money_amount <= 0:
        break

你可以使用这个逻辑

while True:
    if money_amount > 0:
        break