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
11
  • You're going to need to fix the indentation in the question before anyone can help you. Commented Oct 25, 2017 at 1:44
  • Oops, Thanks didn't see that :) Commented Oct 25, 2017 at 1:46
  • its running fine on my machine.. Commented Oct 25, 2017 at 1:46
  • 1
    Is it that you forgot to specify storedPassword in the context? Commented Oct 25, 2017 at 1:47
  • Please update your answer to include the complete error. Commented Oct 25, 2017 at 1:48

2 Answers 2

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: ")
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.