-2

I've finally decided to start learning python as my first language and so far i love it. I;m using python3 along with IDLE3. When writing a simple password prompt im running into a problem :/ script is as follows.

import sys password = input("Enter your password: ") if password != "pword" : sys.exit() print("Password correct") 

Now when i run the script and enter pass as the password i get the following error:

root@kali:~/Desktop# python password1.py Enter your password: pword Traceback (most recent call last): File "password1.py", line 2, in <module> password = input("Enter your password: ") File "<string>", line 1, in <module> NameError: name 'pword' is not defined 

Thanks in advance for your help guys, it's appreciated.

22
  • The code you show has 'pword' (notice the quotes, a string) but the error suggests you have pword (a bareword, undefined variable). If you really do have quotes around it try clearing out your __pycache__ folder. Commented Sep 2, 2016 at 23:35
  • use raw_input instead of input Commented Sep 2, 2016 at 23:37
  • 1
    You're actually using Python 2, aren't you? Commented Sep 2, 2016 at 23:38
  • 1
    Then you are using python 2.7.12 :), maybe you have two versions of python installed Commented Sep 2, 2016 at 23:44
  • 1
    @Adam Smith Thank you so much i'd be lost without your help! Commented Sep 3, 2016 at 0:25

1 Answer 1

1

One of two things is happening. Either

A) your copy is wrong, and your script actually reads

... if password != pword: # note the lack of quotes ... 

or B) you're using Python 2

import sys print(sys.version) # should show 3.x.y per your question, but if it's showing 2.x.y, that'd be why 

The reason Python2 vs Python3 matters is that the input builtin works differently between the two. In Python2, input tries to resolve user input within the namespace, e.g.

# Python2 >>> foo = "bar" >>> spam = input("enter something: ") enter something: foo >>> print(spam) bar 

While Python3 just returns the input as a string

# Python3 >>> foo = "bar" >>> spam = input("enter something: ") enter something: foo >>> print(spam) foo 
Sign up to request clarification or add additional context in comments.

7 Comments

"Should I answer it? No, not if you think it's a duplicate." source
@Polikdir right, but at the time I answered this, it wasn't clear that it was a duplicate. That only came out over the course of the comments with OP. Note that there are two possibilities for what's causing the issue in my answer! :)
password = raw_input("Enter your password: ") worked! Thank you guys
@adam59600 note that that solution is just writing py2 code. MUCH better to keep the code you had and run python 3 instead!
im not sure why my system deafualts to py2.. ive installed py3 did ./configure then make then make install and everything installed successfully... is this a file association issue as previously stated?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.