【问题标题】:Repeated while loop with boolean使用布尔值重复 while 循环
【发布时间】:2021-11-09 08:08:25
【问题描述】:

对不起,如果这被认为是垃圾邮件,我遇到的另一个问题是这个 while 循环无条件运行,我不知道如何解决它。

代码:

while anotherNum == True:
    playerNum = random.randint(1,10)
    total = total + playerNum
    print("")
    print("Your number is ", str(playerNum) + ".")
    print("You have " + str(total) + " in total.")
    print("")
    again = input("Roll another number? <Y or N> ")
    print("")
    if again == "Y":
      anotherNum == True
    else:
      anotherNum == False
      break
  #game finished now
print("Computer got", pcNum)
print("You got", total)
#checking for winner
while anotherNum == False:
  if (total <= 13) and (total > pcNum): 
    print("You,", name, "have won!")
  elif (pcNum <= 13) and (pcNum > total):
    print("The computer has bested you,", name + "!")
  else:
    if (total == pcNum) and (total <= 13):
      print("Draw...")
    elif (pcNum > 13) and (total <= 13):
      print("You,", name + " have won!")
    else:
      print("Both of you have lost. Wow...")

输出:

Your number is  2.
You have 2 in total.

Roll another number? <Y or N> n


Your number is  3.
You have 3 in total.

Roll another number? <Y or N> N


Your number is  3.
You have 3 in total.

Roll another number? <Y or N> N


Your number is  9.
You have 9 in total.

while 循环不会转到#game finished now 评论区域,而是重复该过程,同时仅忽略 total = total + playerNum 命令。

【问题讨论】:

    标签: python while-loop boolean


    【解决方案1】:

    您的代码(经过最少的修正)运行良好:

    import random
    total = 0
    anotherNum = True
    
    while anotherNum == True:
        playerNum = random.randint(1,10)
        total = total + playerNum
        print("")
        print("Your number is ", str(playerNum) + ".")
        print("You have " + str(total) + " in total.")
        print("")
        again = input("Roll another number? <y or n> ")
        print("")
        if again == "y":
            anotherNum = True
        else:
            anotherNum = False
            break
    print("You got", total)
    

    运行代码后得到的输出:

    Your number is  7.
    You have 7 in total.
    
    Roll another number? <y or n>  y
    
    
    Your number is  9.
    You have 16 in total.
    
    Roll another number? <y or n>  y
    
    
    Your number is  10.
    You have 26 in total.
    
    Roll another number? <y or n>  y
    
    
    Your number is  2.
    You have 28 in total.
    
    Roll another number? <y or n>  y
    
    
    Your number is  6.
    You have 34 in total.
    
    Roll another number? <y or n>  n
    
    You got 34
    

    【讨论】:

      【解决方案2】:

      尝试删除break并将==更改为=

      import random
      
      total = 0
      anotherNum = True
      
      while anotherNum == True:
          playerNum = random.randint(1, 10)
          total += playerNum
          print("")
          print(f"Your number is {playerNum}.")
          print(f"You have {total} in total.")
          print("")
          again = input("Roll another number? <Y or N> ")
          print("")
          if again == "Y":
              anotherNum = True  # <- HERE
          else:
              anotherNum = False  # <- HERE
      

      或不带anotherNum 变量:

      import random
      
      total = 0
      
      while True:
          playerNum = random.randint(1, 10)
          total += playerNum
          print("")
          print(f"Your number is {playerNum}.")
          print(f"You have {total} in total.")
          print("")
          again = input("Roll another number? <Y or N> ")
          print("")
          if again == 'N':
              break
      

      【讨论】:

        【解决方案3】:

        我会这样做。

        while anotherNum:
            playerNum = random.randint(1,10)
            total += playerNum
            print("")
            print("Your number is ", str(playerNum) + ".")
            print("You have " + str(total) + " in total.")
            print("")
            again = input("Roll another number? <Y or N> ")
            print("")
            anotherNum = (again == "Y")
        

        这样你可以避免if &lt;blank&gt; then true else false导致你在这里犯错(简单的==而不是=)

        【讨论】:

        • 我还是有同样的问题。
        • 您确定输入的是 "Y" ,而不是 "y" 吗?这将导致问题...尝试再次打印以确保?请注意,第一个 = 只有一个字符,第二个是 "=="
        【解决方案4】:

        你没有改变价值

        if again == "Y":
         anotherNum == True
         else:
         anotherNum == False
         break
        

        你应该使用 1 个等号,因为它是一个赋值

        if again == "Y":
         anotherNum = True
         else:
         anotherNum = False
         break
        

        【讨论】:

          猜你喜欢
          • 2011-08-01
          • 2017-11-20
          • 1970-01-01
          • 1970-01-01
          • 2020-12-03
          • 2013-03-02
          • 2015-05-24
          • 1970-01-01
          • 2016-05-15
          相关资源
          最近更新 更多