Home Python If elif else
Reply: 4

Python If elif else

bhaikal21
1#
bhaikal21 Published in 2018-02-14 03:41:43Z

`

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?

Jeff Storey
2#
Jeff Storey Reply to 2018-02-14 03:51:29Z

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.

Sudheesh Singanamalla
3#
Sudheesh Singanamalla Reply to 2018-02-14 03:48:25Z

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")
Sean
4#
Sean Reply to 2018-02-14 03:49:11Z

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

Josh McMillan
5#
Josh McMillan Reply to 2018-02-14 03:56:55Z

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!")
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.284707 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO