while循环在继续后不会重新执行

while loop doesn't re-execute after continue

我正在创建一个二十一点游戏,我必须让用户下注并确保它不超过总数然后 return 用户下注。我试过这段代码,但它似乎不起作用,while 循环在继续后不会重新执行。

def take_bet(total):
    while True:
        x = int(input('plz : '))
        while True :
            if x <= total :
            break
        if x > total :
            print(f"your bet is higher then your total {total} .")
            continue
        else:
            break
        
    return x

if 语句需要在循环中:

def take_bet(total):
    while True:
        x = int(input('plz : '))
        if x > total :
            print(f"your bet is higher then your total {total} .")
        else:
            break
    return x

您不需要任何其他循环或 if/else 检查

您可以使用一个 while 循环来处理所有事情,如下所示。

def take_bet(total):

    bet = int(input('plz : '))  #Initialise bet.

    while bet > total :
        print(f"your bet is higher then your total {total} .")
        bet = int(input('plz : ')) #Ask again for an allowed bet.

    return bet

无需增加 if 语句和中断的复杂性。