'if/else语句评估中的名称错误'

2024-09-30 18:15:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码如下:

else:
print("that is incorrect!")
choice = input("Would you like to (g)uess again, or (q)uit? ")
if choice == "g":
    guess == input("What animal is this? ")
elif choice == "q":
    game_running = False
else:
    print("That is not a valid option")
    choice = input("Would you like to (g)uess again, or (q)uit? ")

当我运行它时,它为input=g、q或其他任何值返回以下值。在

^{pr2}$

我以前曾多次使用这种评估用户猜测的格式,从未遇到过任何问题。如有任何建议,我们将不胜感激!我在macosx上运行pygame,并将python设置为python3,以使pygame能够更有效地工作。我补充道:

from __future__ import division, absolute_import, print_function, unicode_literals

使python能够使用python3功能。在


Tags: ortoyouinputispygameelsepython3
2条回答

在Python2中,应该使用raw_input()而不是{}来获取未计算的输入。也就是说,input()将在返回给您之前评估用户输入。在

choice = raw_input("Would you like to (g)uess again, or (q)uit? ")

顺便说一句,前两行似乎有一些缩进问题?在

^{pr2}$

编辑:

如果有一天您想切换到python3,请记住这里提到的raw_input()在python3中被重命名为input()。您必须使用eval(input())来获得旧的input()行为(在我看来这有点危险…)

有两个问题,一个是else,另一个我认为是:guess==input(“这是什么动物?”)。跟着下面的零钱走!在

else:
    print("that is incorrect!")
choice = input("Would you like to (g)uess again, or (q)uit? ")
if choice == "g":
    guess = input("What animal is this? ")
elif choice == "q":
    game_running = False
else:
    print("That is not a valid option")
    choice = input("Would you like to (g)uess again, or (q)uit? ")

相关问题 更多 >