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

21 Answered Questions

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

45 Answered Questions

[SOLVED] How to print colored text in Python?

24 Answered Questions

[SOLVED] How can I count the occurrences of a list item?

  • 2010-04-08 13:30:00
  • weakish
  • 1715935 View
  • 1543 Score
  • 24 Answer
  • Tags:   python list count

25 Answered Questions

[SOLVED] How can I safely create a nested directory?

29 Answered Questions

[SOLVED] How can you profile a Python script?

28 Answered Questions

[SOLVED] How can I remove a trailing newline?

  • 2008-11-08 18:25:24
  • RidingThisToTheTop
  • 1853158 View
  • 1698 Score
  • 28 Answer
  • Tags:   python newline trailing

19 Answered Questions

[SOLVED] How can I use threading in Python?

16 Answered Questions

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

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

1 Answered Questions

[SOLVED] Draw shape given x and y coordinates pygame

Sponsored Content