design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

5.6a

LO: To debug simple programs.

Now that we have statements nested inside statements, it would be a good time to look at indenting and how errors could appear while debugging your code.

Reeborg and IDLE will automatically indent your code after you have entered a colon : after defining a function or writing a conditional statement; while, if. The indentation allows you to read the code more clearly - something indented to the same level will belong to the function/statement above it. A single indentation will usually be four spaces:


while not at_goal():
....if object_here(): #belong to the while condition
        take()
....else: #belong to the while condition
        move()
else:
....done() #belong to the while,else condition

			

You can see how these line-up to show what they belong to.


while not at_goal():
    if object_here(): 
........take() #belong to the if condition
    else:
........move() #belong to the if,else condition
else:
    done()

			

When editing your code, it is is sometimes helpful to put the cursor in front of the colon : that follows the function or conditional statement, and then pressing enter/return to get the indent rather than using spaces or the tab key.



Code Examples

Resources