Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code help
#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


Messages In This Thread
Python code help - by PraiseYAH - Jul-05-2018, 09:12 PM
RE: Python code help - by ichabod801 - Jul-05-2018, 09:58 PM
RE: Python code help - by Zombie_Programming - Jul-05-2018, 10:04 PM

Forum Jump:

User Panel Messages

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