0

I have made a script for checking if a variable is the same as an input variable:

def password():
    userPassword = str(input("Type Your Password: "))
    if userPassword == storedPassword:
        print 'Access Granted'
    else:
        print 'Access Denied'
        password()

However whenever I type a letter for the input, it throws a NameError, but it works fine with numbers.

Error:

Traceback (most recent call last):
  File "C:\Users\***\Desktop\Test\Script.py", line 16, in <module>
    password()
  File "C:\Users\***\Desktop\Test\Script.py", line 9, in password
    userPassword = str(input("Type Your Password: "))
  File "<string>", line 1, in <module>
NameError: name 'f' is not defined

2 Answers2

1

You need to use raw_input instead of input on python 2.

Using input attempts to evaulate whatever you pass it. In the case of an integer it just resolves to that integer. In the case of a string, it'll attempt to find that variable name and that causes your error.

You can confirm this by typing a 'password' such as password which would have your input call return a reference to your password function.

Conversely, raw_input always returns a string containing the characters your user typed. Judging by your attempt to cast whatever the user types back into a string, this is exactly what you want.

userPassword = raw_input("Type Your Password: ")
Shadow
  • 8,749
  • 4
  • 47
  • 57
0

For Python 2.7 you need to use raw_input() instead of input(), input() actually evaluates the input as Python code. raw_input() returns the verbatim string entered by the user.

See Python 2.7 getting user input and manipulating as string without quotations

Chazeon
  • 546
  • 2
  • 14