Syntax Error in for loop Python [duplicate] - python

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.

Related

Hash function in Python is not giving the answer I want [duplicate]

This question already has answers here:
hash function in Python 3.3 returns different results between sessions
(3 answers)
Closed 4 months ago.
I am practising Python questions on HackerRank. Here's the question.
This is my solution:
if __name__ == '__main__':
n = int(input())
i = tuple(map(int,input().split()))
print(hash(i))
Desired Output is
3713081631934410656
However, the output I get is
-3550055125485641917
Why is that the case?
Someone tested my code, and it gave them the desired code. I tried copying and pasting my code again. Still didn't work.
Edit
It started working. I had to select Pypy3 (shoutout Bereal). Also, big ups to Chepner for pointing out Python3 Hash functionality.
From the discussion section: "THE TEST is for PYPY3, not Python3. Make sure to change language to Pypy3 !!!"
source: https://www.hackerrank.com/challenges/python-tuples/forum
In general #Inshaullah, it's best to check out the discussion forums on these sites if there's problems along these lines.

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

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)

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

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