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
LO: To know how conditional statements react to events. (2)
We are going to concentrate on using our while loop to check if Reeborg is not at_goal(). This way we can imagine Reeborg carrying out instructions until the goal/home is reached, we can think of this like a game loop - the game is started and then it will carry on until there is a winner/loser:
while not at_goal(): #do what you need to do to complete the world/problem else: done()
If we look at the world Tokens1 we will see that Reeborg is in a world with a token, a place to put that token and a goal (end point). We looked at this world way back in 2.3a, but at that point we were writing all of the instructions for Reeborg - now we are going to get Reeborg to solve this world (and others like it) using conditional statements.
It would be useful just to look back at the flow chart we made back then as we will be including instructions inside our while loop:
Let's put this inside our while loop:
while not at_goal(): move() take() move() put() move() else: done()
To improve this code so it will work no matter how far away the tokens are (see the worlds Tokens2 and Tokens3) we will use another conditional statement inside (nested) the while loop. This is the if statement, from our examples:
1. if I flick the switch, then the light will come on.
2. If the water is boiling, then turn the kettle off.
3. If the number is less than 7, then add 3.
if True: #do something once else: #do something else once
You should be quite comfortable with this statement as it works that same as the while loop - If something is True, then do something, the difference with the while loop is that the instructions will be carried out once. Another difference is that we can use more than one if statement. This example looks at the value of a variable called number:
if number == 7: print("The number is 7") if number == 8: print("The number is 8") else: print("The number is not 7 or 8")
Let's look at another important example:
if number == 7: print("The number is 7") if number > 3: print("The number is bigger than 3") else: print("The number is not 7 or bigger than 3")
If the number is 7 then it will print out 'The number is 7' and 'The number is bigger than 3'. But what if we only want to make one choice? Here we can use elif, which just means else if. This will mean that only one choice will be made - and it will be the first one that Python gets to.
if number == 7: print("The number is 7") elif number > 3: print("The number is bigger than 3") else: print("The number is not 7 or bigger than 3")
This will print out only one choice. This will print out 'The number is 7' because it is the first choice that is True.
Looking back at the world Tokens1 and the conditions that Reeborg is aware of
we can see that Reeborg knows if it is standing on an object (token) through the object_here() - if Reeborg is standing on an object then object_here() is True, if Reeborg is not standing on an object then object_here() is False. How could we use this to solve the Tokens1 world?
Let's use a flow chart to help us think about this:
If Reeborg is standing on a object then pick it up, else move forward. We get Reeborg to check object_here() is True before moving on just in case it is already standing on one in a world. Remember, we are going to put these instructions inside of our original while loop, every time Reeborg is not at the goal/home it will carry out these instructions - our new if statement:
#Tokens1 - notice how the statements are indented! while not at_goal(): if object_here(): take() move() put() move() else: move() else: done()
Open up the worlds Tokens2 and Tokens3 and you will see that the same code will work!