# if / else – weeks 8, 9 and 10

“Decisions, decisions, decisions”

Handout: 5_intro_1_conditionals

## Booleans and their Operators

A boolean is a data type with only two values: True and False. It’s three main operations are

For example: let me try

Now we can take a boolean expression and reduce to its value:

## Karel’s Boolean functions

The following are the boolean functions that Karel uses to sense the world:

When we call any of them, it returns a boolean, e.g., looking at the image we have…

## Conditionals – If / else

Karel uses the sensing functions to make decisions using the ‘if / else’ structure.

The ‘if /else’ structure looks like this:

If the condition is True, Karel runs the commands in the body of the if; otherwise, it runs those in the body of the else. The else portion of the structure is optional.

## Complementary functions

The sensing functions of Karel are limited and do not allow us to talk to him in a natural way. For example, we usually would not ask “not front_is_clear()” but instead we rather ask “wall_in_front()”. Functions that are the opposite of each other are called complementary functions.

The following are the complementary functions of some of Karel’s built-in sensing functions:

We have added these complementary functions to the library.

# Training Missions

## TM1 Life in SoCal – Paving the way

Work it out

Some tiles of the 6-step path to Karel’s house are missing. Karel might have some tiles in his bag but does not know how many he has. Help Karel replace as many tiles as possible, take him home, face him south to see his work, and have him announce he is home.

The initial setup shows that the number of tokens in each cell will be a number between 0 and 1, i.e., some of the cells will have tokens and some others will not. The value will appear when the ‘Play’ button is pressed.

A home square that has a small black dot on one of its sides, like in the case of the one on the right, indicates that the robot must finish the mission facing in that direction.

Before…

…and after.

Hint:

In this mission we use the very useful function safe_put():

Calling safe_put() instead of put() guarantees that the robot will never try to place a token from an empty bag. We will find this function in our library from now on.

## TM2 Life in SoCal – Another touchdown!

Work it out

Training for the day is almost over. Karel will try to score one more touchdown and then head to the showers. However, his teammates left footballs all over the place.

Karel must run straight toward the end-zone which is not farther than 10 steps. If he finds a ball it is path he must pick it up and score. Of course, if there are more than 1 ball in its path he should still pick only one ball and ignore the others. Have Karel announce whether he found a ball and scored or not.

The initial setup shows that the number of tokens in each cell will be a number between 0 and 1, i.e., some of the cells will have tokens and some others will not. The value will appear when the ‘Play’ button is pressed.

The 3 robots show the three possible initial positions of the robot. The program will choose one when the ‘Play’ button is pressed.

Before…

…and after

Hint:

In this mission we use the very useful functions safe_take() and safe_move():

that guarantee that the Karel will not try to drive into a wall or try to take a token from an empty cell. We will find these functions in our library from now on.

## 3rd grade – Strawberries – Dessert

Karel’s 8-cell garden is blossoming with each cell having 0 or 1 strawberries. Have Karel go to his garden and collect as many strawberries as possible, then bring him back home and, finally, orient him to look at his garden.

The initial setup shows that each cell of the garden can have from 0 or 1 strawberries. When we press the ‘Play’ button, Python chooses one of the values for each cell and runs the mission with that scenario.

In this case, only the 2nd, 3rd and 8th cells ended up 1 strawberry; all the other cells have 0 strawberries.

Initial setup showing ranges

A possible draw from the ranges

A possible outcome: “Yay! sweet..”

## 4th grade – Olympic dreams – Biathlon

Work it out

In a team everyone helps in any way they can. Today Karel is assisting the biathlon athletes. They arrive to the shooting station tired and with the adrenaline at an all-time high, and have to shoot at 5 targets nested in 5 bowls, knocking them to the floor. Karel’s job is to replace the targets that have been knocked down and to pick up the ones that lie on the floor.

Karel starts the mission with 5 targets in his bag. Of course, we have no idea which bowls will need to be replenished and which targets are going to be knocked down so we must write our code in such a way that it can handle any result. After this, Karel must go back to the home square, ready for the next round of shooters.

“Biathlon-Weltcup 2006 Antholz 1” by Götz A. Primke from Muenchen, Germany – Biathlon_WC_Antholz_2006_01_Film3_MassenDamen_13. Licensed under Creative Commons Attribution-Share Alike 2.0 via Wikimedia Commons

Before…

…and after.

Hint:

Use the safe_take() function from the library.

Work it out

Karel continues with his dream to become an astrodroid. Today he is training his take-off skills. To prepare the rocket for take-off he has to ‘balance the load’, i.e., he has to make sure that there is a single token in each compartment to have the same weight on both sides of the rocket. Then he has to proceed to the cabin and announce he is ready with a “Blast-off!” message. Karel starts the mission with 16 tokens in his bag.

Before..

…and after

Karel continues with his dream to become an astrodroid. After his successful take-off mission he now has to show that he can land the rocket. He will land it in one of 4 possible pads, at an unknown orientation. Using the known configuration of walls around each path, Karel must figure out where he is with respect to the home square and go to it.