0
0
Pythonprogramming~15 mins

Ternary conditional expression in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Ternary Conditional Expression in Python
📖 Scenario: You are helping a small store decide if a product is affordable for a customer based on their budget.
🎯 Goal: Build a small program that uses a ternary conditional expression to check if a product price is within the customer's budget and print a message accordingly.
📋 What You'll Learn
Create a variable for the product price
Create a variable for the customer's budget
Use a ternary conditional expression to decide if the product is affordable
Print the result message
💡 Why This Matters
🌍 Real World
Stores and shops often check if customers can afford products before suggesting purchases.
💼 Career
Understanding conditional expressions helps in writing clear and concise code for decision-making in software.
Progress0 / 4 steps
1
Set up product price and customer budget
Create a variable called product_price and set it to 120. Then create a variable called customer_budget and set it to 150.
Python
Need a hint?

Use the equals sign = to assign values to variables.

2
Create a message variable using ternary conditional expression
Create a variable called message that uses a ternary conditional expression to set its value to "Affordable" if product_price is less than or equal to customer_budget, otherwise set it to "Too expensive".
Python
Need a hint?

The ternary expression looks like: value_if_true if condition else value_if_false.

3
Print the message
Use print(message) to display the message variable.
Python
Need a hint?

Use the print function to show the message on the screen.

4
Test with a different budget
Change the value of customer_budget to 100 and run the program again to see the new output.
Python
Need a hint?

Remember to update the message variable after changing the budget.