So I'm learning python, from a java background, for my new job, and I've run into the following error.
I tried running this module on the terminal (using mac), then through IDLE. All futile.
userName = input("What is your name?: ")
lengthName = str(len(usernName))
yourName = "\nYour name is " + userName + " and is " + legnthName + " letters long."
print(yourName)
input("\nPress enter to exit")
When this is executed in the terminal and I type for the first input : "John" I get this as a result:
File "[...]", line 1, in <module>
userName = input("What is your name?: ")
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
Similarly, when executed via IDLE:
Traceback (most recent call last):
File "/Users/zachmartin/Desktop/python fails/lol.py", line 2, in <module>
lengthName = str(len(usernName))
NameError: name 'usernName' is not defined
What's going on here?
1 Answer 1
Two problems:
You should be using
raw_input()rather thaninput()(becauseinput()will treat what you type as Python code and try to evaluate it).You have a typo,
userNamevs.usernName(note the extran). (AlsolegnthNamelater on.)