Editor

Ternary Ticket Price

Beginner · Write the code · from Python If-Else: Conditions, Elif, Nested Ifs, and Ternary

From the lesson: Python If-Else: Conditions, Elif, Nested Ifs, and Ternary

Every day you make hundreds of decisions. If it's raining, you grab an umbrella. If the traffic light is red, you stop. If your phone battery is below 20%, you look for a charger. These are all conditional decisions - you check something and act based on the result.

Read the lesson

A movie theater charges $12 for adults (age 18 or over) and $8 for children (under 18). Use the ternary operator to set the price in one line, then print it.

A variable age is given with the value 14.

Expected output:

Ticket price: $8
age = 14
# Use a ternary operator to set the price
price = # your code here
print(f'Ticket price: ${price}')

More if else exercises

All Python exercises · 50 practice problems · Control Flow quiz