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 lessonA 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: $8age = 14
# Use a ternary operator to set the price
price = # your code here
print(f'Ticket price: ${price}')
More if else exercises
- Even or OddBeginner
- Temperature AdvisorBeginner
- Predict the OutputBeginner
- Fix the Grade CalculatorBeginner
- Login ValidatorBeginner
All Python exercises · 50 practice problems · Control Flow quiz