1

NameError: name 'Brandon' is not defined

I have a simple username/password program in Python 2.7.2 and keep getting this dumb error message.

Here's my code:

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"
Brandon
  • 11
  • 2

3 Answers3

7

You should use raw_input instead of input, because input expect that you're entering python code. More accurately though, your trouble is in Username == brandon. brandon would be a variable. 'brandon' would be a string for use in comparison.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • THANKS SO MUCH! I was getting the same thing with raw_input. I didn't want to put brandon in quotes, though, because I thought it would turn into something I didn't want, since I'm only using that for print commands right now. – Brandon Jul 22 '11 at 22:30
  • @Brandon - glad to help. Please be sure to accept answers so that you continue to receive high quality answers from other users. – g.d.d.c Jul 24 '11 at 05:01
4

Use raw_input instead of input.

input is essentially running eval(raw_input(...)). And you don't want to do an eval here.

Also, your password == 42 should probably be password == "42", since raw_input gives back a string.

kenm
  • 23,127
  • 2
  • 43
  • 62
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
3ck
  • 539
  • 1
  • 4
  • 13