Blog Post #28: Mini-Project: Building a Simple Age-Based Access Program

In Post #25, Post #26, and Post #27, we built our conditional logic toolkit with if, else, and elif. Now it’s time to put that powerful decision-making chain into practice with a hands-on mini-project.

Today, we will build a complete program that asks a user for their age and, based on that input, grants them a specific level of access—just like a movie theater or a video game rating system. This project will solidify your understanding of how the if-elif-else structure works in a practical scenario.

Step 1: Planning Our Logic

Before writing any code, it’s a good practice to plan the logic. What are the different outcomes we want to handle? For our access system, let’s define three levels:

  • If age is 18 or greater: Grant “Full Access.”
  • If age is 13 or greater (but less than 18): Grant “Teen Access.”
  • If age is less than 13: Grant “Child Access.”

The order here is important. We should check for the highest age requirement first and work our way down. This ensures that a 20-year-old gets “Full Access” and isn’t incorrectly flagged as having “Teen Access.”

Step 2: Getting the User’s Age

Let’s start by creating a new file named access_checker.py. The first thing our program needs to do is greet the user and ask for their age.

We’ll use the input() function to get the age and immediately use int() to cast it from a string to a number we can perform comparisons on, just as we learned in Post #16.

print("--- Age-Based Access System ---")

# Get the user's age and convert it to an integer
age = int(input("Please enter your age: "))

Step 3: Building the if-elif-else Chain

Now we can translate our plan from Step 1 into an if-elif-else structure. We will add this code directly below the input line.

# Check the age and determine the access level
if age >= 18:
    print("Access granted: Full Access to all content.")
elif age >= 13:
    print("Access granted: Teen Access (PG-13 and below).")
else:
    print("Access granted: Child Access (G-rated content only).")

print("--- Thank you for using the system. ---")

Let’s trace the logic:

  • If a user enters 25, the first condition (age >= 18) is True. The “Full Access” message is printed, and Python skips the rest of the elif and else blocks.
  • If a user enters 15, the first condition is False. Python moves to the elif and checks if age >= 13. This is True, so the “Teen Access” message is printed, and the final else block is skipped.
  • If a user enters 8, both the if and elif conditions are False. Python falls through to the final else block, which acts as our catch-all, and prints the “Child Access” message.

The Complete Program

Here is the final, complete script. You can run it from your terminal (python3 access_checker.py) and test it with different ages to see the logic in action.

print("--- Age-Based Access System ---")

# Get the user's age and convert it to an integer
age = int(input("Please enter your age: "))

# Check the age and determine the access level
if age >= 18:
    print("Access granted: Full Access to all content.")
elif age >= 13:
    print("Access granted: Teen Access (PG-13 and below).")
else:
    print("Access granted: Child Access (G-rated content only).")

print("--- Thank you for using the system. ---")

Experiment by running the program several times. Use the ages 5, 15, 25, and 18 to verify that each path in your logic works as expected.

What’s Next?

You’ve successfully built an interactive program that uses conditional logic to make decisions and provide different outcomes. This ability to control the flow of execution is what makes programming so powerful.

Our current if-elif-else chain works well for a single set of mutually exclusive conditions. But what if you need to ask a follow-up question after a condition has already been met? For this, we can place if statements inside other if statements. In Post #29, we will explore this powerful technique of using nested if statements.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment