How to Create an Age Checker with Python Programming

Here I will be creating a program with Python that will check a user’s age to see if the user is over the drinking age limit. If the user is underage , less than 21, the program will prompt “Access Denied” but if the user is 21 or older the program “Access Granted”.

First, we must create a function call get_age() . When the get_age function is called, the program will run a prompt a place where the user can then enter their age. New to creating Functions, Click here for a quick tutorial!

def get_age():
    while True:
        Age = input("Enter Age: ")
        try:

Next, the main part of the code is created. This code creates the var checker. Checker is used to store the user’s inputted age from the program then the program checks if the checker variable is = < 21 and from there prints out the corresponding statement.

  checker = int(Age)
            if checker >= 21 :
                print("Access Granted!")
                break
            else:
                print("Access Denied! Underage")

Last, I have added an except Value- Error to make sure that the user’s can only input a number. If a letter or symbol is inputted the except will prompt a message telling the user to only enter an integer.

        except ValueError:
            print("Amount must be a number, try again")
    return checker
get_age()

That’s it, lets run the code!

Output :

> Enter amount: 12
Access Denied! Underage


>Enter amount: 33
Access Granted!

Full Code

def get_age():
    while True:
        Age = input("Enter amount: ")
        try:
            checker = int(Age)
            if checker >= 21 :
                print("Access Granted!")
                break
            else:
                print("Access Denied! Underage")
        except ValueError:
            print("Amount must be a number, try again")
    return checker
get_age()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s