Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
indentation tips
#3
I'd say there are two different issues here:
  • the mechanics of inserting/fixing indents
  • understanding how indents work with respect to flow of execution

You've already been given links/advice on how to re-ident code and configure IDLE.

The PEP8 guide on Python formatting and style says to stick with spaces and there should be four spaces per indentation level. Code you copy from elsewhere may not follow either of these conventions but you can configure your editor to fix these for you.

With respect to understanding indentation, I wrote a simple overview for a different forum. This may be too basic for you, but I share it here in case it is of any help.

Indentation is about belonging.

Consider it an hierarchy. Think of how a family tree works (thinking about the bloodline only). Each child (an indent) can have children (another indent). Children of the same parent are indented the same amount.

In code, there are many statements (code) that mean something should be done only if a certain condition is met.

In English, consider:

If it is raining then put on your coat, hat, gloves and wellingtons otherwise just put on your shoes.

Each of the actions:
  • put on coat
  • put on hat
  • put on wellingtons
  • put on gloves (last thing to do logically)
are all part of what you do if it is raining.

In pseudo code:

weather = weather_api(my_location)
if weather == "raining":
    put_on_coat()
    put_on_hat()
    put_on_wellingtons()
    put_on_gloves()
else:
    put_on_shoes()
go_shopping() 
There are lots of places in Python that this indenting of a block of code is used to indicate belonging.

That code above could have had additional conditions within the indented blocks.

Maybe checking the temperature and putting on a warmer coat if cold:

weather, temperature = weather_api(my_location)
if weather == "raining":
    if temperature == "cold":
        put_on_jumper()
        put_on_winter_coat()
        put_on_scarf()
    else:
        put_on_rain_coat()
    put_on_hat()
    put_on_wellingtons()
    put_on_gloves()
else:
    put_on_shoes()
go_shopping()
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
indentation tips - by Pedroski55 - Sep-14-2018, 09:35 AM
RE: indentation tips - by Gribouillis - Sep-14-2018, 10:11 AM
RE: indentation tips - by gruntfutuk - Sep-14-2018, 01:24 PM
RE: indentation tips - by Pedroski55 - Sep-15-2018, 10:55 PM
RE: indentation tips - by Gribouillis - Sep-16-2018, 10:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Any tips to how to find out values? nevlindan 1 750 Apr-27-2023, 09:14 PM
Last Post: deanhystad
  coding improvement tips vaisesumit29 1 1,855 Mar-10-2019, 05:09 PM
Last Post: stullis
  Tips for CLI program with Keybinding system pedropessoa 2 2,629 Nov-21-2018, 09:59 AM
Last Post: Gribouillis
  Organizing tips Demini 1 2,256 Feb-10-2018, 05:32 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020