Getting an error while taking input from an embedded python code - python

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.

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.

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

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.

python error Talentbuddy:EOFError: EOF when reading a line

I wrote some python code on Talentbuddy's code editor which lead to an EOFError: EOF when reading a line, but I ran the same code on python IDLE and everything is ok.
The code is meant to simply sum the number is given by Talenbuddy, but I don't know how to get the number.
def get_sum(a,b):
c=a+b
return c
a=raw_input()
b=raw_input()
print get_sum(a,b)
The error is:
Traceback (most recent call last):
File "eval_get_sum.py", line 3, in
from user_file import get_sum
File "/eval/user_file.py", line 4, in
a=raw_input()
EOFError: EOF when reading a line
Assuming this is "Simple Sum", you aren't supposed to be taking any raw_input. When you click "Check Solution", the system will call your function, providing appropriate arguments a and b; you don't have to call it yourself. Remove the last three lines and test again.

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