我在学习的课程中进行了练习,但这给我一个错误:
"Guess a number between 1 and 100: Traceback (most recent call last):
File "main.py", line 15, in <module>
guess = input("Guess a number between 1 and 100: ")
EOFError: EOF when reading a line"
# Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:
# If a player's guess is less than 1 or greater than 100, say "OUT OF BOUNDS"
# On a player's first turn, if their guess is #within 10 of the number, return "WARM!"
# further than 10 away from the number, return "COLD!"
# On all subsequent turns, if a guess is
# closer to the number than the previous guess return "WARMER!"
# farther from the number than the previous guess, return "COLDER!"
# When the player's guess equals the number, tell them they've guessed correctly and how many guesses it took!
from random import randint
random_number = randint(1,100)
guess_count = 0
guess = input("Guess a number between 1 and 100: ")
while False:
guess_count += 1
if guess_count == 1:
if guess == random_number:
print(f'Congratulations! You have chose the correct number after the first try!')
break
else:
if abs(guess-random_number) < 11:
print("WARM!")
else:
print("COLD!")
else:
old_guess = guess
guess = input("Guess another number between 1 and 100: ")
if guess == random_number:
print(f'Congratulations! You have chose the correct number after {guess_count} tries!')
break
elif abs(random_number - guess) < abs(random_number - old_guess):
print('WARMER!')
elif abs(random_number - guess) > abs(random_number - old_guess):
print('COLDER!')
input("Press anywhere to exit ")
答案 0 :(得分:1)
您得到的原因
跟踪(最近一次通话最近一次):文件“ main.py”,第15行,猜测= input(“猜测1到100之间的数字:”)EOFError:读取行时EOF“
可能是因为实际数字前有空格或换行符。
请在使用前考虑评估用户输入(如果用户输入字符,该怎么办?)。以How can I read inputs as numbers?为例。
也像其他人指出的那样,改变
while False:
至while True:
希望这会有所帮助
答案 1 :(得分:-1)
程序中没有错误,但为false时除外。将其更改为while True。 基本上,您正在获取输入,然后由于false导致程序终止。