I'm taking this Coursera course on linear algebra, which is Python based. I got so far as to produce some initial plots and work with complex numbers on IP Notebook. I am using Windows, and the display of the Notebook is on Chrome.
I got to a command to call a jpeg image on the plot and the line written by the instructor is:
from image import *
I am getting the following error message:
In [7]: from image import *
File "image.py", line 98
print("Hit Enter once the image is displayed.... ", end="")
^
SyntaxError: invalid syntax
I believe that the call may be pointing to the image.py downloaded from this site.
I'll go and assume that in the course they're working with python 3, so the error you're getting is because of the print function.
from image import *
File "image.py", line 98
print("Hit Enter once the image is displayed.... ", end="")
^
SyntaxError: invalid syntax
Either use Python 3, or from __future__ import print_function
It looks to me like the code is designed for Python 3 and uses Python's print function. Anaconda provides Python 3, so try using that instead -- I can import image fine that way.
The precise location of the SyntaxError when I use Python 2 is here:
File "image.py", line 98
print("Hit Enter once the image is displayed.... ", end="")
^
SyntaxError: invalid syntax
which happens because Python2 is trying to print a tuple of values but the second element it gets for the tuple is an assignment.
The docs for your Coursera course tip you off about this (although it doesn't seem obvious) and warn you to use Python 3 and not Python 2.
Related
I'm trying to solve a problem using the following code:
X,Y = map(float, input().split())
if X < Y and not X % 5:
print(Y - X - 0.50)
else:
print(Y)
This code gives me the desired output when I run using IDLE. However, when I try running this code using an interpreter provided by a competitive programming website, I get the following error:
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
EOFError: EOF when reading a line
I tried reading the answers of other similar questions, but none of them seemed to work in my case.
I am not sure of the reason but the program is trying to read after the end of the data. You can solve the problem by exception handling
try:
data = input()
except EOFError:
break
Take another look at the codechef page. Notice the checkbox marked 'Custom Input'. With that checked/ticked a textbox will open where you can put your input lines.
The competitive programming website is likely running python 2. Python 2 treats input() differently than python 3.
You should rather use raw_input() than input().
From the docs:
raw_input() reads a line from input, converts it to a string
(stripping a trailing newline), and returns that.
Your problem can be explained from what was explained here:
In Python 2, raw_input() returns a string, and input() tries to run
the input as a Python expression.
I am working on a calculator program as a part of a much larger project and when I finally thought I had it finished I tested the defined "quit" command. However, it failed and after some research, I came here. What I need to know is how to make the (path) argument have a set, pre-defined path, but also have a variable for the actual file name. eg: /HDD/APPS/(insert variable here).
This is the error and the line that the error occurred on:
File "../../C.py", line 19
if ( not os.path.isfile('/HDD/APPS/'exe)):
^
SyntaxError: invalid syntax
Concatenate two strings:
>>> exe = 'exefile'
>>> '/HDD/APPS/' + exe
'/HDD/APPS/exefile'
More preferably, use os.path.join:
>>> import os
>>> os.path.join('/HDD/APPS/', exe)
'/HDD/APPS/exefile'
I am trying to load an image from a file specified on the command line then process it using the OpenCV command HoughCircles. What happens is that I am opening the file:
img = cv2.imread(argv[0],0)
Then trying to use the following function:
def _getCircles(img):
_circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=1,maxRadius=20)
But returns the error:
cv2.error: error: (-206) Unrecognized or unsupported array type in function cvGetMat
However if I load the file directly ie change argv[0] to the explicit filename everything works perfectly. Any ideas?
In python and most other languages argv[0] contains the program name. Try using argv[1] to get the correct result.
Here is some Python Documentation to help you. Python has an amazing library of documentation I highly recommend using it.
Sorry I am not familar with Python...
It gives me the following error message
File "gen_compile_files_list.py", line 36
print 'java files:', n_src
^
SyntaxError: invalid syntax
I.e. caret points to last quote. What's wrong with it?
OS Windows 7, Python version 3.2.2
On Python 3, print is a function. You need this:
print('java files:', n_src)
print changed syntax between Python2 and Python3; it is now a function.
You would need to change:
print 'java files:', n_src
to
print('java files:', n_src)
Alternatively, you can try converting the code from Python2 to Python3 syntax with the 2to3 tool. Here is more information on the transition if you are interested. This way you can maintain a single code base that works on both versions.
As you are not familiar with python, try installing Python 2 instead and running the code with that.
print is a function in Python 3+. So:
print ('java files:', n_src)
I just switched from python 2 to python 3. One of my python code written with python 2 failed. The error cannot be understand and I don't want to convert my python 2 code into python 3 compatible version. It would be better if I just rewrite the whole program.
However, the resource in google is confusing so I can't find any resource for python 3 telnetlib module. Does anyone know any resource for that?
I attached the error message when I executed the half-way converted python code:
File "E:\Python_Program\telnet.py", line 122, in <module>
tn.write(ftpUsername + "\n")
File "C:\Python32\lib\telnetlib.py", line 277, in write
if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes
If your program is already working in python 2.x, then I think that what you really need is take a look at the Text Vs. Data Instead Of Unicode Vs. 8-bit section in the documentation since it explains why you're getting the traceback in the question:
The biggest difference with the 2.x situation is that any attempt to mix text and data in Python 3.0 raises TypeError