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

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.

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")

Getting an error while taking input from an embedded python code

I have a bash script that invokes python2.6 for reading input , but i'm encountering an error.
Below is the script. I'm running the bash script from terminal
a=1
b=2
python2.6 <<"EOF"
a=int(input('Enter num 1'))
b=int(input('Enter num 2'))
ptint('a=%d ' % a)
print('b=%d ' % b)
EOF
c=$((a + b))
echo "$c"
Enter num 1Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line
3
Enter num 1Traceback (most recent call last):
You pass your script in through stdin and you also want your user input on stdin. input raised it since it got nothing. In any case, you could use a different construct (like process substitution) to "embed" that python code. E.g.:
...
python2 <(cat <<"EOF"
a=int(input('Enter num 1'))
b=int(input('Enter num 2'))
print('a=%d ' % a)
print('b=%d ' % b)
EOF
)
...
That won't fail, but is likely not doing what I assume is you wanted it to do. Take the prints and use them to redefine the values of bash variables.
For that you would have to wrap the whole python call in another pair if $(...) for command substitution. However, that would also gobble your input prompts (making them invisible and confusing the heck out of shell). I do not think input let's you use stderr for prompts to get around that. (It also has another problem of performing eval on the input, which could expose you to whole array of other problems and is probably not a function you want to use in this case).
I am not entirely sure where this script was heading, but I hope this gives you something to go on with.

Python only works in one case, returns syntax error for other cases [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.

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")

Categories

Resources