I'm new to python and have problems with input.
When I'm using command userName = input('What is your name? ')
it says something like this:
>>> userName = input('What is your name? ')
What is your name? Name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Name' is not defined
What I must do with it?
change it to :
userName = raw_input('What is your name?')
In Python 2.x:
raw_input() returns string values and
input() attempts to evaluate the input as command
But in python 3.x, input has been scrapped and the function previously known as raw_input is now input.
The function input() evaluates input as a command--that is why numbers work, but not generic strings that cannot be executed. To achieve attaching a string to a variable, use the more universal raw_input(), which reads everything as a string.
Your new code will now look like
userName = raw_input('What is your name? ')
Best of luck, and happy coding!
Related
I have a problem with input, I include the code and error. The problem is very basic but I can't resolve it.
The code is:
name = input("What is your name? ")
print("Hello ", name)
line 2, in <module>
name = input("What is your name? ")
File "<string>", line 1, in <module>
NameError: name 'luca' is not defined
For python2 you should use:
name = raw_input("What is your name? ")
But may i suggest using python3? Using python3 will not complain about your example.
Edit: Thank you for your helpful answers! I downloaded Python 3.7.0 but you are right, my Mac is running Python 2.7. I have homework now :) Figure out how to get it running 3.7. I will come back if I have more questions. Thank you!
Beginner here. I'm getting NameError when executing in Mac with Python Launcher. When testing in Python 3.7.0 Shell it works ok. I've read other answers to NameError questions, but do not understand what I'm doing wrong. Help is appreciated.
Code used
first_name = input ("Hi, what's your first name? ")
print ("Hi," , first_name)
Error received
Traceback (most recent call last):
File "/Users/imperio/Documents/pythonpractice/Name.py", line 1, in <module>
first_name = input ("Hi, what's your first name? ")
File "<string>", line 1, in <module>
NameError: name 'Imperio' is not defined
This is most likely because you are not executing it using Python 3+.
Please check the output of python -V to see which version you are executing your code with.
On mac you may already have both installed, Python 3 is sometimes aliased under python3 file.py
Here's your program converted to valid Python2:
first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))
You're running Python 2. In Python 2, input executes the input. raw_input just returns the string entered.
Here's an example:
>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w # Variable 'w' doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ') # Raw input just returns the input.
variable? x
x
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 5 years ago.
Whenever I try to execute this code:
name = input("What's your name?")
print("Hello World", name)
By running the command python myprogram.py on the command line, it gives me this error:
What's your name?John
Traceback (most recent call last):
File "HelloWorld.py", line 1, in <module>
name = input("What's your name?")
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
It asks me the name but as soon as I type it and press enter it crashes, what does the error mean?
Thanks.
In Python 2 you should use raw_input instead of input in this case.
Assuming you are using python 2 when you enter just (John) it interprets that as variable. You will either need to enter ("John"), forcing it to see a string, or use name = raw_input() in the first line.
I am currently working through Automate the Boring Stuff with Python and was given this example in Chapter 4 (You can read the page here if you're curious). The code was typed from the example given in the book as suggested and is pasted below. In the book, I am told the response I get is supposed to prompt me to enter a pet name, and if it doesn't match what's in the list I should get a response saying that I don't have a pet by that name.
The problem I run into is that the response I ACTUALLY get is :
Enter a pet name:
Gennie
Traceback (most recent call last):
File "/Users/gillian/Documents/Python/AutomateTheBoringStuffwithPython/Ch4Example1.py", line 3, in <module>
name = str(input())
File "<string>", line 1, in <module>
NameError: name 'Gennie' is not defined
I'm not sure why that would be. I don't see anything different about my code from the example, but something about that error seems not right. Can anyone tell me where I've gone off course?
myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name: ')
name = input()
if name not in myPets:
print('I do not have a pet named ' + name)
else:
print(name + ' is my pet.')
Change input() into raw_input() as you seem to be using python 2.x and this code is written in 3.x.
Find out more about differences here.
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 6 years ago.
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.
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