【发布时间】:2018-01-21 20:00:53
【问题描述】:
我是一名初级程序员,我创建了一个猜谜游戏,但我想合并一个字符串,当用户按下回车键时将打印该字符串,我已经尝试过,但它不起作用。 (返回错误“ValueError: invalid literal for int() with base 10”)当用户按下回车键时。
如果用户不按回车键而只输入整数,程序可以正常工作。
highest = 12
lowest = 6
answer = 9
input("Press enter to play a guessing game")
guess = input("Guess a number from %d " %lowest + "to %d: " %highest)
while (int(guess) != answer) or guess == ():
if int(guess) > answer:
print ("Answer is lower")
elif guess == ():
print ("That's not valid")
else:
print("Answer is higher")
guess = input("Guess again ")
print ("Correct!!")
【问题讨论】:
-
guess == ()永远不会是True。 -
@cᴏʟᴅsᴘᴇᴇᴅ 好吧,如果我放类似
-
@coldspeed str(guess) == ("") 这行得通吗?
-
是的,会的。