Code not compiling in Python3 - python

I wrote a code and it is getting compiled on my PC with Python3. But Showing error while uploading to Codechef server. Please suggest, I am coding for the first time in Python 3.
Traceback (most recent call last):
File "/run-ls7W2DcLmzUs9GNKbLGN/solution.py", line 41, in <module>
l,r,k=map(int,input().split())
File "<string>", line 1
9 23 1
^
SyntaxError: invalid syntax

You're using python2.x which evaluates the string you enter for input. Change the function from input to raw_input and you should be all set.
If you want the code to work for both python2.x and python3.x, you could do a simple little hack like this at the top of your script:
try:
#This raises `NameError` on python3.x since `raw_input` is renamed to `input`
input = raw_input
except NameError:
pass
It's not pretty, but it works (and I've used things like this on occasion). Ultimately, this shadows the builtin input on python2.x, but that's really not a big deal. You probably don't want to be using that builtin for any serious coding anyway.

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.

Error in python asterisk syntax. No idea what is wrong

I'm trying to locally minimize a complicated function using scipy.optimize.minimize. Since I require good gradients in order for the local optimization to be smoothily performed, but the function is so very much complicated in order for the derivatives to be written by hand, I decided to use Autoptim as the middle-man to handle my optimization using the automatic differentiation package Autograd to obtain the gradients.
After I installed the package (as well as Autograd), I opened my python terminal in order to run a few preliminary tests to check whether the installation and the package integration between scipy, autograd and autoptim went smoothily. Then Autoptim raised an error immediately upon import (at the line import autoptim). Since the interpreter gives the full stack of Exceptions raised, I went to the deeper layers to see what line initiated the cascade that halted the interpreter.
The line I found was line 88 of autoptim.py:
87. optim_vars = _convert_to_tuple(optim_vars)
88. precon_optim_vars = precon_fwd(*optim_vars,*args)
89. n_args = len(args)
Python interpreter raised an Invalid Syntax Exception, which means that something in that line is not written "in Python". I checked to see whether there was some unclosed parenthesis and that was not the case. I am using Python 3 so I figured that maybe something on that line was written in Python 2 syntax and it registers wrong for a Python 3 interpreter but as I understand it, the differences between the two versions is quite small and there is some (although not complete) retrocompatibility between the two.
So what gives? What am I missing here?
What is wrong with that line?
Here is the traceback of the import line in the python interpreter
>>> import autoptim
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/autoptim/__init__.py", line 6, in <module>
from .autoptim import minimize # noqa
File "/usr/local/lib/python3.4/dist-packages/autoptim/autoptim.py", line 95
return objective_function(*optim_vars, *args)
^
SyntaxError: invalid syntax
The syntax being used wasn't introduced until Python 3.5 (see PEP 448). You are using Python 3.4.
As a workaround, you could explicitly build the required list to unpack:
return objective_function(*list(optim_vars + args))

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: Use of Input() method for debug

My application (C#/WPF) creates Python code for users who are not programmers - they have a selection of drag & drop items and so they create a sequence that the app translates to Python.
That being said, they'd like to add debug capabilities - the option to inject inputs into their sequence during execution (for example, change myVar's value from 1 to 2). From what I understand, I should be able to translate that into Python's input(). I've been trying to understand how it works and got the following error(using PythonWin):
>>> a = 1
>>> input('--:)')
--:)>>> a=2
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<string>", line 1
a=2
^
SyntaxError: invalid syntax
What am I doing wrong?
(Also, some tutorial for debugging capabilities in Python - including input() will be appreciated.)
Edit
We're working with Python 2.6.6. No need for IronPython - it's executed in Python environment.
According to python documentation, input() method is equivalent to eval(raw_input(prompt)) and eval() can be used to evaluate expressions only.
So for example this code is valid:
>>> a=1
>>> input('--:)')
--:) a+1
2
but a=2 is a statement so you have to use exec() method to execute it, here is an example:
>>> exec(raw_input())
a=2
>>> a
2

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