Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following code, which gives me an indentation error:
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.speed_x = -5
            elif event.key == pygame.K_RIGHT:
                player.speed_x = 5
			elif event.key == pygame.K_a:
                player.shoot(RED)
			elif event.key == pygame.K_s:
				player.shoot(GREEN)
			elif event.key == pygame.K_d: 
				player.shoot(BLUE)
			elif event.key == pygame.K_f: 
				player.shoot(YELLOW)


What I have tried:

I have tried to erase the line, but it doesn't work
Posted
Comments
PIEBALDconsult 15-Aug-24 20:49pm    
Infested with TABs?

If I had to guess, you have a mix of tabs and spaces somewhere in there. That's a common pain point.
 
Share this answer
 
To add to what Pete has said, most editors - or at least the half decent ones - have options for using tabs or spaces, and replacing one with the other. Set that to spaces so that tabs are automatically replaced in the file and your problem should go away, or at least be visible.

Python is a poor language for this very reason: whitespace is significant, and TAB and SPACE are not equivalent: what looks like three spaces on screen could be one, two, or three characters in the file but Python looks only at the file content, and counts each whitespace character as a single whitespace.
Since the number of whitespace characters must exactly match to get the right indentation, it's very easy to get "indentation errors" that you cannot see!

It's understandable - the "size" of a tab can vary both depending on it's position in the line and on the editor settings (most competent editors will allow you to specify a tab at each , 3, 4, ... characters on a line but Python doesn't know which editor you used) but it's a fundamental problem with the language design ... and a good reason why beginners shouldn't learn it! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900