NameError:名称'Brandon'未定义
我在Python 2.7.2中有一个简单的用户名/密码程序,并继续收到这个愚蠢的错误消息。
这是我的代码:
Username = input ("Please enter your username: ")
if Username == brandon:
password = input ("Correct! Please enter password: ")
if password == 42:
print "Access granted!"
else:
print "Wrong Password!"
else:
print "Wrong username"
答案 0 :(得分:8)
你应该使用raw_input而不是input,因为输入需要你输入python代码。但更准确地说,您的问题出在Username == brandon
。 brandon
将是一个变量。 'brandon'
将是一个用于比较的字符串。
答案 1 :(得分:4)
使用raw_input
代替input
。
input
基本上正在运行eval(raw_input(...))
。你不想在这里做eval
。
此外,您的password == 42
应该是password == "42"
,因为raw_input
会返回一个字符串。
答案 2 :(得分:0)
# raw_input() reads every input as a string
# then it's up to you to process the string
str1 = raw_input("Enter anything:")
print "raw_input =", str1
# input() actually uses raw_input() and then tries to
# convert the input data to a number using eval()
# hence you could enter a math expression
# gives an error if input is not numeric eg. $34.95
x = input("Enter a number:")
print "input =", x