好的,我正在用python编写一个成绩检查代码,我的代码是:
unit3Done = str(input("Have you done your Unit 3 Controlled Assessment? (Type y or n): ")).lower()
if unit3Done == "y":
pass
elif unit3Done == "n":
print "Sorry. You must have done at least one unit to calculate what you need for an A*"
else:
print "Sorry. That's not a valid answer."
当我在python编译器中运行它并选择"n"
时,会出现一个错误,说:
"NameError: name 'n' is not defined"
当我选择"y"
时,我会得到另一个NameError
问题是'y'
,但是当我做其他事情时,代码会正常运行。
非常感谢您的帮助
谢谢你。
您正在Python 2上使用^{} function 。请改用^{} ,或者切换到Python 3。
input()
对给定的输入运行eval()
,因此输入n
被解释为python代码,查找n
变量。你可以通过输入'n'
(所以用引号)来解决这个问题,但这很难解决。在Python 3中,
raw_input()
被重命名为input()
,完全替换了Python 2的版本。如果您的材料(书籍、课程笔记等)使用input()
的方式希望n
起作用,那么您可能需要切换到使用Python 3。使用Python 2中的^{} 来获取字符串,python2中的^{} 相当于
eval(raw_input)
。所以,当你在
input
中输入类似n
的内容时,它会认为你在寻找一个名为n
的变量:raw_input
工作正常:有关
raw_input
的帮助:关于
input
的帮助:相关问题 更多 >
编程相关推荐