0
0
Pythonprogramming~15 mins

Assert statement usage in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Assert Statement Usage
📖 Scenario: You are working on a simple program that checks if a user's age is valid for a certain activity. You want to make sure the age is not negative and is within a reasonable range.
🎯 Goal: Build a program that uses assert statements to check the validity of an age value.
📋 What You'll Learn
Create a variable called age with a specific integer value.
Create a variable called max_age to set the maximum allowed age.
Use assert statements to check that age is not negative and does not exceed max_age.
Print a message confirming the age is valid.
💡 Why This Matters
🌍 Real World
Assert statements help programmers catch mistakes early by checking if values are correct while the program runs.
💼 Career
Many software jobs require writing code that safely checks data and assumptions to avoid bugs and errors.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to the integer value 25.
Python
Need a hint?

Use a simple assignment like age = 25.

2
Create the max_age variable
Create a variable called max_age and set it to the integer value 120.
Python
Need a hint?

Use a simple assignment like max_age = 120.

3
Add assert statements to check age
Add two assert statements: one to check that age is greater than or equal to 0, and another to check that age is less than or equal to max_age.
Python
Need a hint?

Use assert age >= 0 and assert age <= max_age.

4
Print confirmation message
Write a print statement that outputs the exact text: "Age is valid."
Python
Need a hint?

Use print("Age is valid.") to show the message.