Python Arena
Step 4 of 5

Conditions at work

A block can hold any code: calculations, assignments and prints. A common pattern changes a variable only in some cases, and then carries on with whatever the variable holds:

price = float(input("Price: "))
if price > 100:
    price = price * 0.9
    print("You get a 10% discount.")
print(f"To pay: {price}")
Input
150
Output
Price: 150
You get a 10% discount.
To pay: 135.0

With a price of 80, the block is skipped, price stays 80.0, and the program prints just To pay: 80.0 after the input.

Several ifs in a row

Each if is checked on its own, so when several conditions are true, several blocks run:

height = int(input("Height in cm: "))
if height >= 100:
    print("You can ride the carousel.")
if height >= 120:
    print("You can ride the bumper cars.")
if height >= 140:
    print("You can ride the roller coaster.")
print("Have fun!")
Input
125
Output
Height in cm: 125
You can ride the carousel.
You can ride the bumper cars.
Have fun!

When one block changes what the next one checks

The if statements run in order, and a later condition sees the variables as the earlier blocks left them. That can catch you out. At this café, an order of 8 or more gets 2 off, and an order of 10 or more also comes with a free cookie. The program looks right, but try it with an order of 11:

total = int(input("Order total: "))
if total >= 8:
    total = total - 2
if total >= 10:
    print("A free cookie comes with your order.")
print(f"To pay: {total}")
Input
11
Output
Order total: 11
To pay: 9

The order was 11, so the customer should get the cookie. But the first block has already lowered total to 9, and the second condition checks that new value. One fix is to keep the price after the discount in a variable of its own, so every condition checks the order as the customer made it:

total = int(input("Order total: "))
to_pay = total
if total >= 8:
    to_pay = total - 2
if total >= 10:
    print("A free cookie comes with your order.")
print(f"To pay: {to_pay}")
Input
11
Output
Order total: 11
A free cookie comes with your order.
To pay: 9

Swapping the two if statements would fix this program too. The variable of its own is the safer habit: every check later in the program still sees the order the customer made.

Your task

Challenge 1 of 3

Orders of 50 or more get 5 off. Fill in the blank so the program takes the discount off the total. If the user types 60, the run looks like this:

Order total: 60
To pay: 55

Challenge 2

A fitness app gives badges for the steps walked in a day: a bronze badge for 5000 steps or more, a silver badge for 10000 or more and a gold badge for 20000 or more. A walker gets every badge they reach. Write a program that asks for the steps, prints each badge earned and then Keep walking!. If the user types 12000, the run looks like this:

Steps today: 12000
Bronze badge
Silver badge
Keep walking!

Challenge 3

In this game, a player with 100 points or more goes up a level, and the 100 points are used up. A player with 150 points or more also finds a bonus chest. But a player with 160 points goes up a level and finds no chest. Fix the program, so that a run looks like this:

Points: 160
You found a bonus chest!
Level 2 with 60 points left