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

时间:2017-09-19 03:52:17

标签: python-3.6

我目前陷入了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)

1 个答案:

答案 0 :(得分:2)

input()以字符串形式返回输入。你的比较是整数。使用:

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

除此之外,最后的精灵和其他人应该缩进更多。