By InuKa


2020-01-14 13:19:41 8 Comments

I'm a beginner in python,

I have this error :

unindent does not match any outer indentation level (<unknown>, line 24) 

This is my code :

  import pygame

#get screen
screen = pygame.display.set_mode((800, 600))

#Set title and logo
pygame.display.set_caption('Space Invaders')
icon = pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)


#Running
running = True
while running:
    import pygame

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

  screen.fill((155 , 0, 0))
  pygame.display.update() 

I tried to fill screen with colors, but above Error appears always. Please help me to fix this.

1 comments

@David Buck 2020-01-14 13:44:34

You have an indentation error in line 1, but I guess that's an error from pasting into Stack Overflow, as it isn't the source of your error message.

As the error message says, you are unindenting the lines:

  screen.fill((155 , 0, 0))
  pygame.display.update() 

as they do not match the indentation of the for above them, nor have you unindented them enough to match the while above, so Python doesn't know where they belong. Ideally all indentations should be either 0 or a multiple of 4 spaces long.

The indentation of the last two lines should presumably be the same as the for loop above it. Also you only need to import pygame once so you shouldn't include that in the loop.

So (assuming you want those two lines inside the while) something like:

import pygame

#get screen
screen = pygame.display.set_mode((800, 600))

#Set title and logo
pygame.display.set_caption('Space Invaders')
icon = pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)

#Running
running = True
while running:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

    screen.fill((155 , 0, 0))
    pygame.display.update() 

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] How to change indentation mode in Atom?

16 Answered Questions

[SOLVED] How do I fix the indentation of an entire file in Vi?

  • 2009-02-03 05:15:25
  • mmcdole
  • 272655 View
  • 612 Score
  • 16 Answer
  • Tags:   vim vi indentation

8 Answered Questions

[SOLVED] How can I use tabs for indentation in IntelliJ IDEA?

21 Answered Questions

[SOLVED] Turning off auto indent when pasting text into vim

9 Answered Questions

[SOLVED] How to fix/convert space indentation in Sublime Text?

9 Answered Questions

[SOLVED] How to change the indentation width in emacs javascript mode

6 Answered Questions

[SOLVED] How to correct indentation in IntelliJ

6 Answered Questions

[SOLVED] How do I force Sublime Text to indent two spaces per tab?

5 Answered Questions

[SOLVED] Can a line of Python code know its indentation nesting level?

7 Answered Questions

[SOLVED] How to automatically indent source code?

Sponsored Content