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

3.4a

LO: To call functions from another function (nested functions) (1).

In 3.3a we wondered if there was any repetition present in our code. Here we will assume that you noticed.

Just looking at the maze we can see that Reeborg will have to 'jump' several times

Let's look at the code again:

#Year group or class
#name				
#date
#hurdles 2

#plotting out the route we can see that we will need to turn right at the top of the jump
def turn_right():
    turn_left()
    turn_left()
    turn_left()

#write a function to complete the world
def hurdles():
    move()
    turn_left()    #here we start the jump
    move()         #move up the wall...
    turn_right()   #over the top...
    move()         #over the top...
    turn_right()   #on the way down...
    move()         #on the way down...
    turn_left()    #finish the jump, straighten out.
    move()
    turn_left()    #here we start the jump again
    move()         #move up the wall...again
    turn_right()   #over the top...again
    move()         #over the top...again
    turn_right()   #on the way down...again
    move()         #on the way down...again
    turn_left()    #finish the jump, straighten out. again
    move()
    turn_left()    #here we start the jump again
    move()         #move up the wall...again
    turn_right()   #over the top...again
    move()         #over the top...again
    turn_right()   #on the way down...again
    move()         #on the way down...again
    turn_left()    #finish the jump, straighten out. again
    move()
    
#delete the indent to complete the function and then call it using its name
hurdles()

			

I think we might have found some repetative, anoying typing!

I'm sure we would all feel better if we saw that in a flow chart:

Next lesson we will turn that flow chart into a function and get hurdling!

Code Examples

Resources