0

I am currently trying to develop a game. I am using python IDLE which does not run my code as it says that I have an syntax error in my code.

The code works using notepad, however not on the Python IDLE. Could someone please help and advise?.

SCREEN      = display.set_mode((800,600))
FONT = "fonts/space_invaders.ttf"
IMG_NAMES   = ["ship", "ship", "mystery", "enemy1_1", "enemy1_2", "enemy2_1", "enemy2_2",
                "enemy3_1", "enemy3_2", "explosionblue", "explosiongreen", "explosionpurple", "laser", "enemylaser"]
IMAGES      = {name: image.load("images/{}.png".format(name)).convert_alpha()
                for name in IMG_NAMES}

for name in IMG_NAMES} - This is giving a invalid syntax error which for highlighted.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
user5963943
  • 31
  • 1
  • 8
  • 4
    You (or your IDLE installation) are probably using Python version <=2.6, which does not have dictionary comprehensions yet. Add `import sys; print sys.version` somewhere in your code to find out. – tobias_k Feb 22 '16 at 16:05
  • 1
    Did you try to write the line without the linebreak? – albert Feb 22 '16 at 16:12
  • am using python 26 version. however its still not working. – user5963943 Feb 22 '16 at 16:24
  • 2
    Well, it is not working _because_ you are "using python 26 version". See my answer. – tobias_k Feb 22 '16 at 16:26
  • how can I make it work then. – user5963943 Feb 22 '16 at 16:28
  • You _really_ should add some more information to the question (please use the edit function, not as a comment): (1) How exactly do you run it "from IDLE" and "from notepad" (particularly the latter is a bit obscure), and (2) what are the exact Python versions that are shown in those two cases respectively? – tobias_k Feb 22 '16 at 17:20
  • I have now installed python 2.7 and the error no longer exist however my game crashes and stops responding as I have two man graphical images. It does work when I run basic not graphical games. I want to now see if it runs from the windows CMD, however how can I achieve this?. – user5963943 Feb 22 '16 at 17:28

1 Answers1

1

This is just a wild guess, but it seems like you (or at least your IDLE installation) use a Python version of 2.6 or older. Dictionary comprehensions were first introduced in Python 2.7.

To find out, you can this at the top of your code and see what it prints when you run it from within IDLE or from the command line ("using notepad"):

import sys
print sys.version

If you are indeed using Python 2.6 and iff you can not upgrade it you can change your dictionary comprehension to a generator expression inside the dict function:

IMAGES = dict((name, image.load("images/{}.png".format(name)).convert_alpha())
              for name in IMG_NAMES)

Example:

>>> {x: x**2 for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>> dict((x, x**2) for x in range(10))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • how can I update phyton and pygame so that I have the correct version installed. – user5963943 Feb 22 '16 at 16:26
  • 1
    @user5963943 I don't have IDLE installed right now, but there should be an option to select the python version to use. If not, download a more recent version of IDLE, or any other decent Python IDE. Since it works "using notepad", you must already have a more up to date version of Python installed, it's just IDLE that's using a different version. – tobias_k Feb 22 '16 at 16:28
  • @user5963943 Just installed IDLE and there seems not to be an option to select the Python version, i.e. each version of IDLE is "hard-wired" to a specific version of Python. In this case, either get a new IDLE or (recommended) get a proper IDE or simply an editor with syntax highlighting. There are [quite a few to choose from](http://stackoverflow.com/questions/81584/what-ide-to-use-for-python). – tobias_k Feb 22 '16 at 16:31
  • When I install the latest version of python then I have issues with pygame as it does not import that's why I installed this version. How can install the correct python version with pygames aswell. – user5963943 Feb 22 '16 at 16:32
  • @user5963943 You said that it works when you are "using notepad". How did you start it then? What did `sys.version` print? And what "latest version of python" do you have now? As far as I know, PyGame only works with Python 2.x, so if you have Python 3.x now, that's too new. Make sure you get Python 2.7 and a decent IDE. – tobias_k Feb 22 '16 at 16:35
  • If I install phython 2.7 will this work then. – user5963943 Feb 22 '16 at 16:35
  • @user5963943 "If I install phython 2.7 will this work then." It should, unless you run into another problem then. Only one way to find out. Or change the dictionary comprehension in the way I showed, if it's too much of a hassle. – tobias_k Feb 22 '16 at 16:36
  • Can I run folder from notepad as well as I have three different folders called images and fonts as well as game. – user5963943 Feb 22 '16 at 17:08
  • @user5963943 Well, that entirely depends on _how_ you "run from notepad". Last time I checked, notepad did not have a "run as python script" function, so you are probably editing your code in notepad and running from command line? What command are you using? But yes, you can access those folders, if you execute it from the same folder as when you run it from IDLE. – tobias_k Feb 22 '16 at 17:19
  • How do I do then. When I run from IDLE my game just says not responding and crashes the whole application. Please help how I can run it from note pad or the windows CMD. – user5963943 Feb 22 '16 at 17:22
  • @user5963943 I can not help you any further without you providing more information. Until then, just use the `IMAGES = dict(...)` workaround. – tobias_k Feb 22 '16 at 17:27
  • I have now installed python 2.7 and the error no longer exist however my game crashes and stops responding as I have two man graphical images. It does work when I run basic not graphical games. I want to now see if it runs from the windows CMD, however how can I achieve this?. – user5963943 Feb 22 '16 at 17:29