【问题标题】:Infinite loop after input from the user用户输入后无限循环
【发布时间】:2020-09-22 17:35:50
【问题描述】:
#python game

import random

a = random.randint(1,100)

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

while True :

    if a == b:
        print ("you guessed it correct \n you won the game!!")
        break
        
    elif a>= b:
        print ("you guesssed it too high")
        
    else :
        print ('you guessed it too low')

【问题讨论】:

  • 你必须在循环中而不是在循环之前获取输入。

标签: python python-3.x list python-requests


【解决方案1】:

您必须在循环中而不是在循环之前获取输入:

a = random.randint(1,100)

# b = int (input("guess the number")) <--- DELETE

while True :

    b = int (input("guess the number")) # <--- INSERT

    if a == b:
        print ("you guessed it correct \n you won the game!!")
        break        
    elif a >= b:
        print ("you guesssed it too high")        
    else:
        print ('you guessed it too low')

【讨论】:

    【解决方案2】:

    将输入放入循环中。

    import random
    
    a = random.randint(1,100)
    
    while True :
        b = int (input("guess the number"))
        if a == b:
            print ("you guessed it correct \n you won the game!!")
            break
    
        elif a>= b:
            print ("you guesssed it too high")
    
        else :
            print ('you guessed it too low')
    

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 2021-03-12
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2020-09-07
      相关资源
      最近更新 更多