Mirco Susan
Mirco Susan

Reputation: 45

elif statements wont print if True

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")

Upvotes: 0

Views: 65

Answers (3)

Amish Gupta
Amish Gupta

Reputation: 151

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 the below one so that you don't need to write int(Age) everywhere:

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

Upvotes: 0

Subhranil Sarkar
Subhranil Sarkar

Reputation: 1

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")

Upvotes: 0

Vladimir Fokow
Vladimir Fokow

Reputation: 3883

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.

Upvotes: 1

Related Questions