Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code help
#1
So here's the assignment:

Write a program that provides proper output given the following constraints:
• If the person’s age is between 6 and 18 they are told “Remain in school.”
• If the person’s age is 16 or over they are told “You are eligible to drive!”
• If the person’s age is 18 or over they are told “You can vote and/or join the military!”
• If the person’s age is 21 or over they are told “You can drink alcohol too.”
• If the person’s age is 65 to 70 they are told “You are eligible to retire!”
• If the person’s age is the same as the oldest living human, ask “How did you live so long?”
• If the person’s age is greater than the oldest living human or less than zero only “Invalid age was entered.” is displayed.

This is what I have so far:
age = int(input("Enter your age"))

if age >= 6 and age <= 18:
    print("Remain in school")
if age >=16:
    print("You are eligible to drive!")
    if age >= 18:
        print("You can vote and/or join the military!")
    if age >= 21:
        print("You can drink alcohol too.")
    if age >= 65 and age <=70:
        print("You are eligible to retire!")
    if age > 70 and age <=112:
        print("How did you live so long?")
else:
    print("Invalid age was entered.")
I'm having trouble with this code; first issue is when the input is between 6 and 15, I get the output of "Remain in school" and "Invalid age was entered" instead of just the "Remain in school".
The second issue is when the input is > 112 I'm getting "You are eligible to drive!", "You can vote and/or join the military!" and "You can drink alcohol too." How do I get the output to display just "Invalid age was entered."?
Reply
#2
Look at your conditionals and follow the numbers through.

If you enter 8, it trips the first conditional, but not the second conditional, so the else is triggered. The else clause only cares about the most recent if clause (the second one). A solution here would be to change else to elif age > 112, or if age > 112 if you haven't covered elif yet.

If you enter 120, that is greater than 112, so it triggers the second if clause, which means the else is not triggered. So you need to change the if clause to prevent that.

Two things to note: You have not covered the situation of an age less than zero. That means you probably want to make the else clause into an if clause. Second, these two are equivalent:

if age > 70 and age <=112:
    print("How did you live so long?")
if 70 < age <=112:
    print("How did you live so long?")
It's called chaining your comparisons.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It's best if you nest the if statements like this,

age = int(input("Enter your age"))

if age >= 113:
    print("Invalid age.")
 
if age >= 6 and age <= 18:
    print("Remain in school")
if age >=16:
    print("You are eligible to drive!")
    if age >= 18:
        print("You can vote and/or join the military!")
        if age >= 21:
            print("You can drink alcohol too.")
            if age >= 65:
                print("You are eligible to retire!")
                if age >= 71 and age <=112:
                    print("How did you live so long?")
I also added in the first if statement if age >= 113
this way if the age is 113 or above, the if statement is now false and executes the else which contains the print("Invalid Age"). The logic behind the nested if-statement is when the age is 6 or greater it'll run that and then check if the age is greater than or equal to 16 and if it is, then it'll run that as well then check if the age is greater than or equal to 18... so on and so forth. Hopefully this helps :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020