Error in python code - SyntaxError: invalid syntax [duplicate] - python

This question already has answers here:
Django Python Dictionary comprehension giving syntax error
(1 answer)
Alternative to dict comprehension prior to Python 2.7
(1 answer)
Closed 5 years ago.
I need to use a code written by someone else in Python a while back, and I get a SyntaxError: invalid syntax in this line:
tfileServices = {n: s.fileName for n, s in process.services.iteritems() if s.type_() == 'TFileService'}
I am not really sure what is wrong about it, so can someone tell me why is it not working (it supposedly worked for him)? Thank you! (I am not sure what to google for to find out what is wrong, as I am not sure how the actual syntax should look like)

Related

F string prefix in python giving a syntax error [duplicate]

This question already has answers here:
f-strings giving SyntaxError?
(7 answers)
Closed 5 years ago.
I have a variable called method, it's value is POST but when I try to run print(f"{method} method is used.") it keeps giving a syntax error at the last double quote and I can't find the reason on why it does this. I am using python 3.5.2.
F-strings were a new feature introduced in Python 3.6, so of course they're not going to work in 3.5.2.

Python " ".join() syntax error [duplicate]

This question already has answers here:
Why is parenthesis in print voluntary in Python 2.7?
(4 answers)
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 6 years ago.
I haven't done much with Python, but the code-academy course has seemed to give me some enjoyment however when I have taken my project to the actual python compiler I get an error compiling. Highly possible that code-academy just hasn't updated to a more recent version, as I've seen here that people have used this previously.
C:\Python>battleship.py
File "C:\Python\battleship.py", line 10
print " ".join(row)
^
SyntaxError: invalid syntax
For some reason I am having issues placing the source in the question, as this is my first question, so ill describe the code. Using an empty list there is a "board" created to play battleship on, with a for loop that loops through a set range appending < board.append["O"] * 5 >. Then it loops through each row in the array and then uses < print " ".join(row) >
How can I get by this and is there an alternative way that is better?
Edit: Python version 3.5.2

Syntax Error in for loop Python [duplicate]

This question already has an answer here:
Python: SyntaxError
(1 answer)
Closed 9 years ago.
this runs fine on my local machine, but as soon as I deploy it the service fails saying invalid syntax "for"
data = {k: request.form[k] for k in request.form.iterkeys()}
SyntaxError: invalid syntax
Appreciate the help on this, it's been a very long time since I did anything with Python and this one has got me stumped.
As others have mentioned, this is probably a version issue. Try:
data = dict((k,request.form[k]) for k in request.form.iterkeys())
The python version on your local machine might be different from the version you have on the server.
Dict comprehensions are a relatively new addition to 2.x. Convert it to a genex that generates (key, value) pairs and pass it to the dict() constructor.

Showing Syntax Error while try to output the dictionary or a list value [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
I am a amature in Python,using the python33 version.The problem i am facing while i want to get output of a list of dictionary just like that.
dict = {'name':'Tanvir','Position':'Programmer'};
print dict['name'];
if i run the code then there showing a syntax error.same thing is happening for the list also.
Please help me to fix the problem.
Thanks in advance.
In Python 3, print is a function, so you need print(dict[name]). You also don't need the semicolons. You also need to read the Python tutorial to learn the basics first.

Syntax error its there but where? [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
Once again Python's helpful error messages make me eat my keyboard.
I've checked the whole script forwards and backwards but can't find any "syntax error(s)".
Is there a decent debugger for Python or a helpful website which is able to scan my code for errors?
C:\Users\Daapii\Desktop>"foo.py"
File "C:\Users\Daapii\Desktop\foo.py", line 26
print "test"
^
SyntaxError: invalid syntax
Python why u no tell me more!
If this is Python 3, then the print function now requires parenthesis. Try print("test").
(EDIT: If Python continues to make you want to eat your keyboard, this might help.)
print is a function in Python 3:
print ("test")
Find other changes here: docs

Categories

Resources