Reputation: 487
First of all, I apologize for what is probably a VERY noob question.
That being said, I would like to understand why Python sometimes target a ":" in a script and throws the "invalid syntax" error. It usually happens after I add some stuff to the script, and then it keeps saying that putting a ":" after an "if" statment is wrong.
To verify how this happens, I created a different "if" statment to put in the place in which the old one was, and the error still happened. I also tried to copy-paste the "if" statment in another script, and it worked.
for event in pygame.event.get():
The line above was located in line 35 of the following script:
import pygame, sys display_width = 800 display_height = 600 BLUE = (0,0,255) DISPLAY = pygame.display.set_mode ((display_width,display_height)) Player_img = pygame.image.load("spacecore.png") clock = pygame.time.Clock() Player_Img = pygame.image.load("role.png") def game_loop(): Game = True while Game == True: pygame.display.update() DISPLAY.fill(BLUE) clock.tick(60) def player(x,y): DISPLAY.blit(Player_Img,(x,y)) mod_x = mod_y = 0 x += mod_x y += mod_y player((10,100) for event in pygame.event.get(): if event.type == pygame.key.get_pressed(): if event.key == pygame.K_LEFT: mod_x = 3 elif event.key == pygame.K_LEFT: mod_x = 3 elif event.key == pygame.K_LEFT: mod_x = 3 elif event.key == pygame.K_LEFT: mod_x = 3 else: mod_x = mod_y = 0 if event.type == pygame.QUIT: Game = False game_loop() pygame
Upvotes: 0
Views: 1004
Reputation: 29906
You are missing a closing parenthesis here:
player((10,100)
When there are unclosed parentheses (()
[]
{}
) in a line, python will interpret the following lines as all of them were written in the same line. This way you can break large array initializations or function calls with lots of arguments in multiple lines, without the need to escape the newline at the end of every line.
You can write this:
max(
[1, 2, 3, 4, 5],
key=math.sin
)
instead of this:
max( \
[1, 2, 3, 4, 5], \
key=math.sin \
)
Hovewer, in your situation it hides the real syntactic error, and produces a false one, the :
is the first term that cannot be written in the argument list of a function call. The terms before ((10, 100) for event in pygame.event.get()
) could be a generator expression, if the colon wasn't there, thats why it is failing on the colon and not on the missing parenthesis.
Upvotes: 2