bhaikal21
bhaikal21

Reputation: 17

Python If elif else

`

is_summer = True
 print("Values 1-12 correlate to the months of the year")
 while is_summer == True:
     _month = int(input("Enter the month of the year: "))
     if _month == 4 or 5 or 6 or 7 or 8:                 
         is_summer = int(input("It is summer, what is the temperature?: "))
         if is_summer in range(60,101):                  
             print("The squirrels are playing")
         else:
             print("The squirells are not playing")      
     elif _month == 1 or 2 or 3 or 9 or 10 or 11 or 12:   
         is_summer = int(input("It is winter, what is the temperature?: "))
         if is_summer in range(60,91):
             print("The squirrels are playing")           
         else:
             print("The squirrels are not playing")
    `

My code will not go down to the elif statement if i enter 1,2,3,9,10,11,or 12. Are my nested if statements done incorrectly, or is it something else?

Upvotes: 1

Views: 558

Answers (4)

Josh McMillan
Josh McMillan

Reputation: 734

Your first if statement is basically saying if _month is 4 – great – or if 5 or 6 or 7 or 8 will evaluate to True. In an if statement in Python, positive integers will always evaluate to True

if 1:
  print("hello!")

will always print hello!. Your if statement needs to look like this, instead:

if _month == 4 or _month == 5 or _month == 6 or _month == 7 or _month == 8:
  print("hurrah!")

This is getting needlessly verbose, however – we can simplify this using comparison operators like less-than (>) and greater-than (<), like so:

if _month < 3 and _month > 9:
  print("hurrah!")

Upvotes: 1

Jeff Storey
Jeff Storey

Reputation: 57192

The reason your code is not executing as you expect is because you are not actually checking if _month equals each of the numbers.

if _month == 1 or 2 or 3 is not the same as if _month == 1 or _month == 2 or _month == 3.

Think of the first as if (_month == 1) or (2) or (3). _month == 1 evaluates to False, but 2 or 3 are non-zero values that evaluate to True, so the first if is always taken.

Upvotes: 1

Sean
Sean

Reputation: 11

Your issue is with your if statements.

if _month == 4 or 5 or 6 or 7 or 8: 

This checks if _month == 4, or if 5 is truthy, or if 6 is truthy etc.

You want to be doing:

if _month == 4 or _month ==5 or _month == 6 or _month == 7 or _month == 8:

or more concise

if 4 <= _month <= 8:

Do the same for your elif. Although if you know _month is goin to be from 1 to 12, then really it can probably just be an else, instead of an elif

Upvotes: 1

Sudheesh Singanamalla
Sudheesh Singanamalla

Reputation: 2297

Your conditional statements work as follows which is the reason why the condition is not always true for the input you provide.

>>> month = 4
>>> month == 4 or 5 or 6
True
>>> month = 5
>>> month == 4 or 5 or 6
5

You could use the in operator to check whether the _month exists in the list of values you're using the or check for. The in operator works as follows

month = 1
month in [1,2,3,4,5] # True
month in [2,3,4,5,6] # False

Therefore you could change your program to be

is_summer = True
print("Values 1-12 correlate to the months of the year")
while is_summer == True:
    _month = int(input("Enter the month of the year: "))
    if _month in [4,5,6,7,8]:                 
        is_summer = int(input("It is summer, what is the temperature?: "))
        if is_summer in range(60,101):                  
            print("The squirrels are playing")
        else:
            print("The squirells are not playing")      
    elif _month in [1,2,3,9,10,11,12]:   
        is_summer = int(input("It is winter, what is the temperature?: "))
        if is_summer in range(60,91):
            print("The squirrels are playing")           
        else:
            print("The squirrels are not playing")

Upvotes: 1

Related Questions