Input in python not recognized - python

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

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.

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.

Why this nameError keeps going on?

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.

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