【发布时间】:2016-06-14 19:37:18
【问题描述】:
如果用户的猜测大于或小于随机生成的值,Python 循环不希望循环返回。它要么退出循环,要么创建一个无限循环。我哪里错了?
import random
correct = random.randint(1, 100)
tries = 1
inputcheck = True
print("Hey there! I am thinking of a numer between 1 and 100!")
while inputcheck:
guess = input("Try to guess the number! " )
#here is where we need to make the try statement
try:
guess = int(guess)
except ValueError:
print("That isn't a number!")
continue
if 0 <= guess <= 100:
inputcheck = False
else:
print("Choose a number in the range!")
continue
if guess == correct:
print("You got it!")
print("It took you {} tries!".format(tries))
inputcheck = False
if guess > correct:
print("You guessed too high!")
tries = tries + 1
if guess < correct:
print("You guessed too low!")
tries = tries + 1
if tries >= 7:
print("Sorry, you only have 7 guesses...")
keepGoing = False
【问题讨论】:
-
您的循环位于
inputcheck,您在if 0 <= guess <= 100块中将其设置为False。如果你这样做,你为什么希望它继续运行?
标签: python loops if-statement while-loop infinite-loop