Skip to the content

Project: ticket price

Python · Unit 2: Conditionals

You can store a result in a variable and change it with another if before the return.

That way a second rule applies on top of the first one.

def total(price, member):
    if member:
        price = price - 2
    return price

print(total(15, True))
print(total(15, False))

Prints

13
15

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Write the code

    At the movies, kids under 12 pay 9, people 60 or older pay 11 and everyone else pays 15. Write ticket_price(age), which gives back the price.

  2. 2. Write the code

    On Tuesdays the ticket is half price for everyone. Write ticket_price(age, day), which works out the price by age as before and, if day is "Tuesday", divides it by 2.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →