[Tutor] Indentation Frustrations
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Nov 18 19:43:16 EST 2020
On 18/11/2020 22:19, Nathaniel Dodson wrote:
> typing it line for line from a tutorial. I get to a certain point and it
> just gives me indentation error after indentation error.
I'm always leery of making comments about formatting errors because
it's often the email system messing things up. However, since the
bulk of your code looks OK I'll take a guess on this one.
But, in principle for any future posts, please tell us where you
are copying the code from (se we can compare) and what editing
tool you are using (since many of them do their own formatting!)
> and when I get to the part to assign keyboard key functions for movement,
> it gives me "expected an indented block" error (and always highlights the
> "i" in the if statement in red):
That's because you don't have an indented block after your
if statements. An if statement (or any other statement ending with
a colon) expects an indented line or block of code. If you don't
have one you get an indentation error.
> while run:
> pygame.time.delay(100)
>
> for event in pygame.event.get():
> if event.type == pygame.QUIT:
> run = False
See, here you have the run assignment indented after the if line.
Presumably no errors in this case.
> keys = pygame.key.get_pressed()
> if keys[pygame.K_LEFT]:
But here there are no lines. You can add a placeholder like
pass, or even a random number, anything at all that python
can evaluate.
> if keys[pygame.K_RIGHT]:
>
> I'm not getting what's going on. I know about mixing tabs with spaces, and
> I'm not doing that. That's what I learned through Googling the issue.
Read the error carefully, they are very specific.
Mixing tabs/spaces or using inconsistent indent levels would give
specific messages about that particular sin.
You are getting an "expected an indented block" message which
is exactly what is wrong. You need ab indented block. Any block
will do but it needs to be there.
Incidentally, this may be an issue with your tutorial where the
author has only provided an outline instead of runnable code.
In that case you will likely find the content of the missing
blocks a little later in the tutorial.
Naughty author....!
But I've been guilty of the same in the past. :-(
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list