【问题标题】:python while loop in a while loop ignores the print after winning the gamepython while循环中的while循环在赢得比赛后忽略打印
【发布时间】:2020-12-29 15:31:20
【问题描述】:

我的程序有问题。我正在开发一个程序,可以让你玩猜正确数字的小游戏。问题是如果你猜对了,它不会打印出来:“你猜对了”。该程序将不会继续,并将停留在正确的号码上。只有当您必须多次猜测时才会发生这种情况。我尝试将 else 更改为 break 命令,但没有成功。 有人给点建议吗?

这是我用来测试它的:

最小的数:1
最大数:10
你能猜到多少次:10

如果您尝试猜测正确的数字 2 或 3 次(如果您需要更多猜测,可能更多)它不会打印出您赢了。

import random

#counts the mistakes
count = 1

#askes to give up a minimum and maximum to guess between
minimum = int(input("what is the smallest number? "))
maximum = int(input("what is the biggest number?  "))

#askes how many times u can guess in total
amount = int(input("How many times can you guess?  "))


#random number between the 2 variables minimum and maximum
x = random.randrange(minimum, maximum)


guess = int(input("guess the number: "))

#while loop until the guess is the same as the random number
while guess != x:

    #this is if u guessed to much u get the error that you've guessed to much
    while count < amount:
            if guess > x:
                print("this is not the correct number, the correct number is lower \n")
                guess = int(input("guess the number: "))
                count += 1

            elif guess < x:
                print("this is not the correct number, the correct number is higher \n")
                guess = int(input("guess the number: "))
                count += 1

    else: print("\n \nYou Lost, You've guessed", x, "times\n")
    break
#this part is not working, only if you guess it at the first time. it should also print this if you guessed it in 3 times
else: print("You guessed it correctly", x)


test = (input("this is just a test if it continues out of the loop "))
print(test)

【问题讨论】:

    标签: python


    【解决方案1】:

    主要问题是,一旦guess == xcount &lt; amount 运行了一个永远不会停止的while 循环,因为您不会接受新的输入。此时,你应该将break 退出循环,这也将结束外循环

    【讨论】:

      【解决方案2】:

      您只需使用while loop 即可,如下所示:

      import random
      
      #counts the mistakes
      count = 1
      
      #askes to give up a minimum and maximum to guess between
      minimum = int(input("what is the smallest number? "))
      maximum = int(input("what is the biggest number?  "))
      
      #askes how many times u can guess in total
      amount = int(input("How many times can you guess?  "))
      
      
      #random number between the 2 variables minimum and maximum
      x = random.randrange(minimum, maximum)
      
      
          #this is if u guessed too much u get the error that you've guessed too much
      while count <= amount:
        guess = int(input("guess the number: "))
      
        if guess > x:
            print("this is not the correct number, the correct number is lower \n")
            count += 1
      
        elif guess < x:
            print("this is not the correct number, the correct number is higher \n")
            count += 1
      
        else: 
          print("You guessed it correctly", x)
          break
      
      if guess!=x:
        print("\n \nYou Lost, You've guessed", count, "times\n")
      

      【讨论】:

        【解决方案3】:

        正如 Lukas 所说,你创造了一种情况,你进入了一个你永远无法逃脱的循环,因为你不再询问。

        您可以尝试的一种常见模式是故意创建一个 while 循环,该循环将运行并运行,直到您明确地break 退出它(因为玩家猜了太多次,或者因为他们猜对了)。此外,您可以在 while 循环内,而不是在几个地方只要求对代码的一部分进行猜测。

        这是我对您的代码的调整 - 做您想做的许多方法之一:

        import random
        
        #counts the mistakes
        count = 0
        
        #asks to give up a minimum and maximum to guess between
        minimum = int(input("what is the smallest number? "))
        maximum = int(input("what is the biggest number?  "))
        
        #asks how many times u can guess in total
        amount = int(input("How many times can you guess?  "))
        
        #random number between the 2 variables minimum and maximum
        x = random.randrange(minimum, maximum)
        
        #while loop until the guess is the same as the random number
        while True:
        
            if count < amount:
        
                guess = int(input("guess the number: "))
        
                #this is if u guessed to much u get the error that you've guessed to much
                if guess > x:
                    print("this is not the correct number, the correct number is lower \n")
                    count += 1
        
                elif guess < x:
                    print("this is not the correct number, the correct number is higher \n")
                    count += 1
                else:
                    print("You guessed it correctly", x)
                    break
            else:
                print("\n \nYou Lost, You've guessed", x, "times\n")
        

        PS:你已经接近让它工作了,很高兴能达到你所做的那样!

        【讨论】:

          【解决方案4】:

          当猜到的数字正确时,不再检查此条件,因此程序挂起:

          while guess != x:
          

          您如何检查是否相等作为第一个条件,如果为真则跳出循环:

          import random
          #counts the mistakes
          count = 1
          
          #askes to give up a minimum and maximum to guess between
          minimum = int(input("what is the smallest number? "))
          maximum = int(input("what is the biggest number?  "))
          
          #askes how many times u can guess in total
          amount = int(input("How many times can you guess?  "))
          
          
          #random number between the 2 variables minimum and maximum
          x = random.randrange(minimum, maximum)
          
          
          guess = int(input("guess the number: "))
          
          if guess == x:
              print("You guessed it correctly", x)
          
          else:
              while count < amount:
                  if guess > x:
                      print("this is not the correct number, the correct number is lower \n")
                      guess = int(input("guess the number: "))
                      count += 1
          
                  elif guess < x:
                      print("this is not the correct number, the correct number is higher \n")
                      guess = int(input("guess the number: "))
                      count += 1
                  else:
                      print("You guessed it correctly", x)
                      break
              else:
                  print("You guessed too many times")
          
          

          【讨论】:

            猜你喜欢
            • 2016-09-08
            • 1970-01-01
            • 2017-04-29
            • 2017-04-29
            • 2019-11-07
            • 2014-11-02
            • 2021-10-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多