如果用戶的猜測大於或小於隨機生成的值,Python循環不想循環回來。它要麼退出循環,要麼創建一個無限循環。我哪裏錯了?對不起,如果我的格式糟糕,第一次海報。Python 3.4:while循環不循環
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',您在設置爲'FALSE' '如果0 <=猜測<= 100'塊。如果你這樣做,你爲什麼期望它繼續運行? – Blckknght