【问题标题】:Ending a while loop if a variable is less than or equal to zero in Python如果 Python 中的变量小于或等于零,则结束 while 循环
【发布时间】:2015-10-15 18:40:30
【问题描述】:

对于一个学校项目,我一直在制作一个游戏,该游戏允许用户猜测足球比赛的最终结果并能够投注预测。如果他们的余额小于或等于零,我一直试图打破 while 循环,但一直无法这样做。整个 Python 代码比下面显示的要广泛得多,但这是唯一的问题。

tries = int(input("How many attempts would you like : "))

while balance > tries:
   while guesses < 2:

        print ("\nYour current balance is : £",balance)

        gameresultguess = input("\nWho will win - Home, Away, or Draw : ")
        bettingchoice = input("Would you like to bet this round - Yes or No : ")

        while True:
            if str.lower(bettingchoice) == "yes":
                bet = int(input("How much would you like to bet : "))
            if bet > balance: break
            print ("You have insuficient funds to bet this much, please try again")

        guesses +=1

        hometrue = random.randint(1,7)
        awaytrue = random.randint(1,5)

        if hometrue > awaytrue:
             gameresulttrue = "home"

        if awaytrue > hometrue:
             gameresulttrue = "away"

        if hometrue == awaytrue:
             gameresulttrue = "draw"

        if str.lower(gameresultguess) == gameresulttrue:
             print ("Correct! Nice guess! The final score was ",hometrue," : ",awaytrue)
             if str.lower(bettingchoice) == "yes":
                  balance = (balance + bet)
                  print ("Well played! Your bet has been doubled and added to your balance")
             if str.lower(bettingchoice) == "no":
                  print ("Unlucky... Should have placed a bet")

        else:
             print ("Unlucky! The final score was : ",hometrue," : ",awaytrue)
             if str.lower(bettingchoice) == "yes":
                  balance = (balance - bet)
                  print ("Oops... Better luck next time")

print ("Ouch... You went bankrupt. Try coming back when you have more money")

【问题讨论】:

  • 我不知道你在哪里试过这个。您的代码中还有其他 break 语句,因此应该直截了当地说 if balanace

标签: python python-3.x if-statement while-loop


【解决方案1】:

当您调用 break 时,它会跳出您当前的循环。

while True:
    if str.lower(bettingchoice) == "yes":
        bet = int(input("How much would you like to bet : "))
    if bet > balance: break
    print ("You have insuficient funds to bet this much, please try again")

在您的示例中,我相信您指的是余额低于下注的循环,您正在尝试结束游戏?

为此,您可以将此逻辑变成一个函数并返回下注的“结果”。或者,如果您希望保留当前结构,您可以引发异常并捕获以打印最终的“破产”消息。

class InsufficientFundError(Exception): pass

try:
    while balance > tries:
        while guesses < 2:
            ...
            while True:
                ...
                if bet > balance:
                    raise InsufficientFundError()
except InsufficientFundError:
    print("Ouch... You went bankrupt. Try coming back when you have more money")

【讨论】:

    【解决方案2】:

    “如果他们的余额小于或等于零,我一直试图打破 while 循环,但我无法这样做。”

    如果余额小于或等于零,您要中断哪个 while 循环?我假设您发布的是“最大”的,因为如果有人可以继续以负余额下注是没有意义的。

    为此,您必须将其设置为初始状态。试试:

    while balance > 0:
    

    这样,当用户达到负余额或用完钱时,嵌套在此循环下的“投注过程”将停止。

    【讨论】:

      【解决方案3】:

      感谢您的帮助!

      阅读答案后,我发现这只是我早早打破陈述的错误。很抱歉这个问题。

      再次感谢:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-23
        • 2021-11-09
        • 1970-01-01
        • 2016-01-12
        • 1970-01-01
        • 2016-04-10
        • 1970-01-01
        相关资源
        最近更新 更多