2017-09-19 22 views
0

我目前被困在Python3练习中,我似乎无法找到我做错了什么。林不得不编写一个程序,提示用户输入月份和日期,使用这种算法,Python 3+打印哪个季节是由用户输入确定

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) 

回答

2

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

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

除此之外,最后elifs和其他应该缩进更多。

+0

omg ...我不敢相信我错过了。我已经更新了代码并修复了缩进,并且工作正常。谢谢。我为自己失踪而生气。所以非常基本 – Matticus