Why this nameError keeps going on? - python

I'm a C programmer, and started with python today, just for a test, I throw some very simple code, and I get this very crazy nameError, I've saw a couple of then here, but they dont seem to relate.
person = input('Enter your name: ')
print('Hello', person)
This is what I get at the terminal:
C:\Users\Matt\Desktop\Python>python input.py
Enter your name: Matheus
Traceback (most recent call last):
File "input.py", line 3, in <module>
person = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'Matheus' is not defined
Does anybody knows how can I fix that?

You are using Python 2, and in Python 2 the usage of input runs an eval on the input. So when you enter what you think is a string, Python tries to evaluate this.
From the help of input:
input(...)
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
Demo of input:
>>> a = input()
2
>>> type(a)
<type 'int'>
Notice quotes have to be used to get the string:
>>> a = input()
"bob"
>>> type(a)
<type 'str'>
You are better off using raw_input in Python 2. As is this is even the method that is used in Python 3 (but Python 3 calls the method input).
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
raw_input demo:
Notice quotes don't have to be used.
>>> a = raw_input()
bob
>>> type(a)
<type 'str'>

Python 2 tries to interpret calls to input as code. Try raw_input() instead and it should work fine.

Related

Error: must be str, not int when passing input to strptime [duplicate]

I have the following python code:
print 'This is a simple game.'
input('Press enter to continue . . .')
print 'Choose an option:'
...
But when I press Enter button, I get the following error:
Traceback (most recent call last):
File "E:/4.Python/temp.py", line 2, in <module>
input('Press enter to continue . . .')
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
P.S. I am using python IDLE version 2.6 on Windows 7.
Related problem in IPython: Why does the IPython REPL tell me "SyntaxError: unexpected EOF while parsing" as I input the code?
For Python 2, you want raw_input, not input. The former will read a line. The latter will read a line and try to execute it, not advisable if you don't want your code being corrupted by the person entering data.
For example, they could do something like call arbitrary functions, as per the following example:
def sety99():
global y
y = 99
y = 0
input ("Enter something: ")
print y
If you run that code under Python 2 and enter sety99(), the output will 99, despite the fact that at no point does your code (in its normal execution flow) purposefully set y to anything other than zero (it does in the function but that function is never explicitly called by your code). The reason for this is that the input(prompt) call is equivalent to eval(raw_input(prompt)).
See here for the gory details.
Keep in mind that Python 3 fixes this. The input function there behaves as you would expect.
In Python 2, input() strings are evaluated, and if they are empty, an exception is raised. You probably want raw_input() (or move on to Python 3).
In Python 2.x, input() is equivalent to eval(raw_input()). And eval gives a syntax error when you pass it an empty string.
You want to use raw_input() instead.
If you use input on Python 2.x, it is interpreted as a Python expression, which is not what you want. And since in your case, the string is empty, an error is raised.
What you need is raw_input. Use that and it will return a string.

Why NameError name 'my_input_value' is not defined (Python 2.7) [duplicate]

This question already has answers here:
How do I use raw_input in Python 3?
(9 answers)
Closed 16 days ago.
What is the difference between raw_input() and input() in Python 3?
The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input(), and the old input() is gone, but can easily be simulated by using eval(input()). (Remember that eval() is evil. Try to use safer ways of parsing your input if possible.)
In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression.
Since getting a string was almost always what you wanted, Python 3 does that with input(). As Sven says, if you ever want the old behaviour, eval(input()) works.
Python 2:
raw_input() takes exactly what the user typed and passes it back as a string.
input() first takes the raw_input() and then performs an eval() on it as well.
The main difference is that input() expects a syntactically correct python statement where raw_input() does not.
Python 3:
raw_input() was renamed to input() so now input() returns the exact string.
Old input() was removed.
If you want to use the old input(), meaning you need to evaluate a user input as a python statement, you have to do it manually by using eval(input()).
In Python 3, raw_input() doesn't exist which was already mentioned by Sven.
In Python 2, the input() function evaluates your input.
Example:
name = input("what is your name ?")
what is your name ?harsha
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
name = input("what is your name ?")
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
In the example above, Python 2.x is trying to evaluate harsha as a variable rather than a string. To avoid that, we can use double quotes around our input like "harsha":
>>> name = input("what is your name?")
what is your name?"harsha"
>>> print(name)
harsha
raw_input()
The raw_input()` function doesn't evaluate, it will just read whatever you enter.
Example:
name = raw_input("what is your name ?")
what is your name ?harsha
>>> name
'harsha'
Example:
name = eval(raw_input("what is your name?"))
what is your name?harsha
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
name = eval(raw_input("what is your name?"))
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
In example above, I was just trying to evaluate the user input with the eval function.
I'd like to add a little more detail to the explanation provided by everyone for the python 2 users. raw_input(), which, by now, you know that evaluates what ever data the user enters as a string. This means that python doesn't try to even understand the entered data again. All it will consider is that the entered data will be string, whether or not it is an actual string or int or anything.
While input() on the other hand tries to understand the data entered by the user. So the input like helloworld would even show the error as 'helloworld is undefined'.
In conclusion, for python 2, to enter a string too you need to enter it like 'helloworld' which is the common structure used in python to use strings.
If You want to ensure, that your code is running with python2 and python3, add input() function at the beginning of your script:
from sys import version_info
if version_info.major == 3:
pass
elif version_info.major == 2:
try:
input = raw_input
except NameError:
pass
else:
print ("Unknown python version - input function not safe")

Input in python not recognized

I was adding an input function in one of my python programs an hour ago, and using:
location = input()
I got this extremely annoying error message which I do not understand: "hi" is not recognised. PS. "hi is what i wrote in the input box".
I don't get why input() is no longer working. It should be working the exact same way as it did before. I went into my main python folder and tried to run another program from a while ago that had an input in it, and got the same result - all programs with input() in it worked before! Just why dosen't python recognise what the input function is anymore?? Its really strange.
I have scoured the internet for an answer, and with no result, I have come to stack overflow for help.
Meanwhile, I can do absolutely nothing about it. I am SO confused right now.
As Mehdi Pourfar stated, you should use raw_input in python2 and input in python3.
python2
> help(input)
input(...)
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
python3
> help(input)
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.
...
Example of input in python2
>>> def foo(): print('foo called')
...
>>> input('> ')
> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'bar' is not defined
>>> input('> ')
> foo
<function foo at 0x7f24ff7ee6e0>
>>> input('> ')
> foo()
foo called

How I can take text input from console using python3? [duplicate]

This question already has answers here:
How do I use raw_input in Python 3?
(9 answers)
Closed 22 days ago.
What is the difference between raw_input() and input() in Python 3?
The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input(), and the old input() is gone, but can easily be simulated by using eval(input()). (Remember that eval() is evil. Try to use safer ways of parsing your input if possible.)
In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression.
Since getting a string was almost always what you wanted, Python 3 does that with input(). As Sven says, if you ever want the old behaviour, eval(input()) works.
Python 2:
raw_input() takes exactly what the user typed and passes it back as a string.
input() first takes the raw_input() and then performs an eval() on it as well.
The main difference is that input() expects a syntactically correct python statement where raw_input() does not.
Python 3:
raw_input() was renamed to input() so now input() returns the exact string.
Old input() was removed.
If you want to use the old input(), meaning you need to evaluate a user input as a python statement, you have to do it manually by using eval(input()).
In Python 3, raw_input() doesn't exist which was already mentioned by Sven.
In Python 2, the input() function evaluates your input.
Example:
name = input("what is your name ?")
what is your name ?harsha
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
name = input("what is your name ?")
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
In the example above, Python 2.x is trying to evaluate harsha as a variable rather than a string. To avoid that, we can use double quotes around our input like "harsha":
>>> name = input("what is your name?")
what is your name?"harsha"
>>> print(name)
harsha
raw_input()
The raw_input()` function doesn't evaluate, it will just read whatever you enter.
Example:
name = raw_input("what is your name ?")
what is your name ?harsha
>>> name
'harsha'
Example:
name = eval(raw_input("what is your name?"))
what is your name?harsha
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
name = eval(raw_input("what is your name?"))
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
In example above, I was just trying to evaluate the user input with the eval function.
I'd like to add a little more detail to the explanation provided by everyone for the python 2 users. raw_input(), which, by now, you know that evaluates what ever data the user enters as a string. This means that python doesn't try to even understand the entered data again. All it will consider is that the entered data will be string, whether or not it is an actual string or int or anything.
While input() on the other hand tries to understand the data entered by the user. So the input like helloworld would even show the error as 'helloworld is undefined'.
In conclusion, for python 2, to enter a string too you need to enter it like 'helloworld' which is the common structure used in python to use strings.
If You want to ensure, that your code is running with python2 and python3, add input() function at the beginning of your script:
from sys import version_info
if version_info.major == 3:
pass
elif version_info.major == 2:
try:
input = raw_input
except NameError:
pass
else:
print ("Unknown python version - input function not safe")

Why does input() give a SyntaxError when I just press enter?

I have the following python code:
print 'This is a simple game.'
input('Press enter to continue . . .')
print 'Choose an option:'
...
But when I press Enter button, I get the following error:
Traceback (most recent call last):
File "E:/4.Python/temp.py", line 2, in <module>
input('Press enter to continue . . .')
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
P.S. I am using python IDLE version 2.6 on Windows 7.
Related problem in IPython: Why does the IPython REPL tell me "SyntaxError: unexpected EOF while parsing" as I input the code?
For Python 2, you want raw_input, not input. The former will read a line. The latter will read a line and try to execute it, not advisable if you don't want your code being corrupted by the person entering data.
For example, they could do something like call arbitrary functions, as per the following example:
def sety99():
global y
y = 99
y = 0
input ("Enter something: ")
print y
If you run that code under Python 2 and enter sety99(), the output will 99, despite the fact that at no point does your code (in its normal execution flow) purposefully set y to anything other than zero (it does in the function but that function is never explicitly called by your code). The reason for this is that the input(prompt) call is equivalent to eval(raw_input(prompt)).
See here for the gory details.
Keep in mind that Python 3 fixes this. The input function there behaves as you would expect.
In Python 2, input() strings are evaluated, and if they are empty, an exception is raised. You probably want raw_input() (or move on to Python 3).
In Python 2.x, input() is equivalent to eval(raw_input()). And eval gives a syntax error when you pass it an empty string.
You want to use raw_input() instead.
If you use input on Python 2.x, it is interpreted as a Python expression, which is not what you want. And since in your case, the string is empty, an error is raised.
What you need is raw_input. Use that and it will return a string.

Categories

Resources