【问题标题】: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')