Python 3+ 打印哪个季节由用户输入决定

Python 3+ Printing which season is determined by user input

我目前被困在一个 Python3 练习中,我似乎找不到我做错了什么。我必须编写一个程序来提示用户一个月和一天,使用这个算法,

if month is 1,2 or 3 season = winter
else if month is 4,5,6 season = spring
else if month is 7,8,9 season = summer
else if month is 10,11,12 season = fall
if month is divisble by 3 and day >= 21
if season is winter, season = spring
else if season is spring, season = summer
else if season is summer, season = fall
else season = winter

这是我的代码到目前为止的样子。

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

season = ""

if month == 1 or month == 2 or month == 3:
    season = "Winter"

elif month == 4 or month == 5 or month == 6:
    season = "Spring"

elif month == 7 or month == 8 or month == 9:
    season = "Summer"

elif month == 10 or month == 11 or month == 12:
    season = "Fall"

if month % 3 == 0 and day >= 21:
    if season == "Winter":
        season = "Spring"
elif season == "Spring":
    season = "Summer"
elif season == "Summer":
    season = "Fall"
else:
    season = "Winter"

print("Season is ", season)

我在输入后收到回溯错误。我确定它是一些非常小的东西,我没有捕捉到。有任何想法吗? 我感谢你的时间和帮助。

编辑:更新代码

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

season = ""

if month == 1 or month == 2 or month == 3:
    season = "Winter"

elif month == 4 or month == 5 or month == 6:
    season = "Spring"

elif month == 7 or month == 8 or month == 9:
    season = "Summer"

elif month == 10 or month == 11 or month == 12:
    season = "Fall"

if month % 3 == 0 and day >= 21:
    if season == "Winter":
        season = "Spring"
    elif season == "Spring":
        season = "Summer"
    elif season == "Summer":
        season = "Fall"
    else:
        season = "Winter"

print("Season is ", season)

input() returns 输入为字符串。您的比较是与整数进行的。使用:

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

除此之外,最后一个 elif 和 else 应该缩进更多。