Home > Net >  elif statements wont print if True
elif statements wont print if True

Time:08-15

I'm making an app that will tell you what generation you belong to but can't get the elifs to print just the if statement.

Age = input('What year where you born? ')

if int(Age) >= 2001:
    print("Generation Z/Boomlets")
elif int(Age) == range(1981, 2000):
    print("Generation Y/Millennium")
elif int(Age) == range(1965, 1980):
    print("Generation X")
elif int(Age) == range(1946, 1964):
    print("Baby Boomers")
elif int(Age) == range(1927, 1945):
    print("Mature / Silents")
elif int(Age) == range(1901, 1926):
    print("GI Generation")

CodePudding user response:

You can convert your input to int right at the beginning (and won't need to do it in every if):

age = int(input('What year where you born? '))

Replace your == signs with in.

elif age in range(1981, 2000):
    ...
  • there is a convention in Python: variable names should start with a smaller-case letter.

CodePudding user response:

Integer can't be equal to range(). So, you've to check if the given year is in the range()

Age = input('What year where you born? ')

if int(Age) >= 2001:
    print("Generation Z/Boomlets")
elif int(Age) in range(1981, 2000):
    print("Generation Y/Millennium")
elif int(Age) in range(1965, 1980):
    print("Generation X")
elif int(Age) in range(1946, 1964):
    print("Baby Boomers")
elif int(Age) in range(1927, 1945):
    print("Mature / Silents")
elif int(Age) in range(1901, 1926):
    print("GI Generation")

CodePudding user response:

Wherever you have used "== range()", do any one of the below:

  1. Replace "==" with "in".
  2. int(Age) >= 1981 and int(Age)<2000.

P.S:

Age = input('What year where you born? ')

change the above code to so that you don't need to write int(Age) everywhere:

Age = int(input('What year where you born? '))
  • Related