Pick Activity: the if/else program

Two numbers in, one answer out. The sliders stand in for typing at the two input() prompts. Slide them and watch which branch of the program runs.

Inputs

The program prints

What is the chance of rain, 0-100? 20
What is the temperature? 72
Go Running

In the real program, input() stops and waits for you to type a number. int() turns what you typed into a whole number so it can be compared with >= and <.

The Python program

rain = int(input("What is the chance of rain, 0-100? " ))
temp = int(input("What is the temperature? " ))
if temp >= 80 and rain < 50 :
print("Play VolleyBall" )
elif temp >= 80 and rain >= 50 :
print("See a Movie" )
elif temp >= 32 and temp < 80 and rain < 50 :
print("Go Running" )
elif temp >= 32 and temp < 80 and rain >= 50 :
print("Try Yoga" )
else:
print("Read a Book" )

Python reads the conditions from the top and runs the first one that is True. Everything after it is skipped without being checked. If none are True, the else runs.

Every rule on this page was typed by a person who already knew the answer. On the next page the computer is given only examples, a temperature, a chance of rain, and what this program printed, and has to discover the rules on its own. Try sliding to exactly 80°, exactly 32°, or exactly 50% rain and watch which branch flips.