Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

How do I find the season from the month + date? Python

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...