[Python] How to use if and else statements to achieve Age Classifier program

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have an assignment in my Python class. It says:


    Write a program that asks the user to enter a person's age. The program should display a message indicating whether the person is an infant, child, teenager, or adult. Here are the following guidelines:

    • If the person is 1 year old or less, he or she is an infant.
    • If the person is older than 1 year, but younger than 13 years, he or she is a child.
    • If the person is at least 13 years old, but less than 20 years old, he or she is a teenager.
    • If the person is at least 20 years old, he or she is an adult.
    ​

    Here's what I have so far. I'm assuming you use if and else statements.

    age = int(input('Please enter a persons age.'))
    if age <= 1: print('The person is an infant.')
    else: print('The person is not an infant.')
    if age > 1 and age > 13: print('The person is a child.')
    else: print ('The person is not an infant.')
    if age <= 13 and age > 20: print('The person is a teenager.')
    else: print ('The person is not a teenager.')
    if age >=20: print ('The person is an adult.')


    Problem is, when I enter a number, example, the number '4', the program runs as so:

    The person is not an infant.
    The person is not an infant.
    The person is not a teenager.


    That is all it reads to me. So how do I fix this? Because I thought I was on the right track.

    Continue reading...

Compartilhe esta Página