Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I spent way to much time on this, I'm pretty new to python. If someone could help the get the season from the month, and day that would be great, in the current example I'm just trying to get the month working, but if anyone can assist with adding the days that would be great!

    month = int(input("Enter a month: "))
    day = int(input("Enter a day: "))

def season(month):
    if (month == "12" or month == "01" or month == "02" or month == "03"):
        return "winter"
        
    elif(month == "04" or month == "05"):
        return "spring"
        
    elif(month =="06" or month=="07" or month == "08" or month == "09"):
        return "summer"
        
    elif(month =="10" or month=="11"):
        
        else:
            
        return "fall" 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

Don't check every single possible value of month. Just do an inequality. As norie pointed out, your user inputs are converted to integers anyways so this works perfectly for you.

month = int(input("Enter a month: "))
day = int(input("Enter a day: "))

def season(month):
    if (month == 12 or 1 <= month <= 4):
        return "winter"   
    elif (4 <= month <= 5):
        return "spring" 
    elif (6 <= month <= 9):
        return "summer"
    else:
        return "fall"
    
print(season(month))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...