尝试运行此代码时,我在第6行上不断收到语法错误。我试图弄清楚缩进,并浏览了该站点上的相关问题,但无济于事。任何帮助将不胜感激!谢谢!
#program that reads month and day and outputs season
input_month = input()
input_day = int(input())
#input reading for spring
if ((input_month == 'March') and (20 <= input_day <= 31))
or ((input_month == 'June') and (1 <= input_day <= 20))
print('Spring')
#input reading for summer
elif (input_month == 'June') and (21 <= input_day <= 30)
or (input_month == 'September') and (1 <= input_day <= 30)
print('Summer')
#input reading for Autumn
elif (input_month == 'September') and (22 <= input_day <= 30)
or (input_month == 'December') and (1 <= input_day <= 20)
print('Autumn')
#input reading for winter
elif (input_month == 'December') and (21 <= input_day <= 31)
or (input_month == 'January') and (1 <= input_day <= 31)
or (input_month == 'February') and (1 <= input_day <= 28)
or (input_month == 'March') and (1 <= input_day <= 19)
print('Winter')
编辑:该错误读取为“ EOL错误”,该错误过去意味着我未定义变量或未正确编写变量。似乎并非如此。
谢谢大家的帮助!
这是更新的工作代码,包括所有月份:
#program that reads month and day and outputs season
input_month = input()
input_day = int(input())
#input reading for spring
if input_month == 'March' and 20 <= input_day <= 31\
or input_month == 'April' and 1 <= input_day <= 30\
or input_month == 'May' and 1 <= input_day <= 31\
or input_month == 'June' and 1 <= input_day <= 20:
print('Spring')
#input reading for summer
elif input_month == 'June' and 21 <= input_day <= 30\
or input_month == 'July' and 1 <= input_day <= 31\
or input_month == 'August' and 1 <= input_day <= 31\
or input_month == 'September' and 1 <= input_day <= 30:
print('Summer')
#input reading for Autumn
elif input_month == 'September' and 22 <= input_day <= 30\
or input_month == 'October' and 1 <= input_day <= 31\
or input_month == 'November' and 1 <= input_day <= 30\
or input_month == 'December' and 1 <= input_day <= 20:
print('Autumn')
#input reading for winter
elif input_month == 'December' and 21 <= input_day <= 31\
or input_month == 'January' and 1 <= input_day <= 31\
or input_month == 'February' and 1 <= input_day <= 28\
or input_month == 'March' and 1 <= input_day <= 19:
print('Winter')
#if input is not valid
else:
print('Invalid')
答案 0 :(得分:0)
这是语法正确的代码(带有反斜杠):
input_month = input()
input_day = int(input())
if input_month == 'March' and 20 <= input_day <= 31 or input_month \
== 'June' and 1 <= input_day <= 20:
print('Spring')
elif input_month == 'June' and 21 <= input_day <= 30 or input_month \
== 'September' and 1 <= input_day <= 30:
print('Summer')
elif input_month == 'September' and 22 <= input_day <= 30 \
or input_month == 'December' and 1 <= input_day <= 20:
print('Autumn')
elif input_month == 'December' and 21 <= input_day <= 31 or input_month \
== 'January' and 1 <= input_day <= 31 or input_month == 'February' \
and 1 <= input_day <= 28 or input_month == 'March' and 1 \
<= input_day <= 19:
print('Winter')
但是,有一些方法可以使该解决方案更具可读性,pythonic和紧凑性。例如,像这样:
MONTHS = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"]
BOUNDARIES = [((3, 20), (6, 20), "Spring"),
((6, 21), (9, 21), "Summer"), # FIXED THE UPPER BOUNDARY
((9, 22), (12, 20), "Autumn"),
((12, 21), (12, 31), "Winter"),
((1, 1), (3, 19), "Winter")]
input_month = input()
input_day = int(input())
input_date = (MONTHS.index(input_month) + 1, input_day)
for lower, upper, season in BOUNDARIES:
if lower <= input_date <= upper:
print(season)
break
或(效率较低的方式):
SEASONS = {"Spring": [
("March", (20, 32)),
("June", (1, 21)),
],
"Summer": [
("June", (21, 31)),
("September", (1, 31)),
],
"Autumn": [
("September", (22, 31)),
("December", (1, 21)),
],
"Winter": [
("December", (21, 32)),
("January", (1, 32)),
("February", (1, 29)),
("March", (1, 20)),
]
}
input_month = input()
input_day = int(input())
for season in SEASONS:
for month, day_ranges in SEASONS[season]:
if input_month == month and input_day in range(*day_ranges):
print(season)
else:
continue
break
答案 1 :(得分:0)
您首先忘记在每个if和else语句的末尾使用分号。 接下来,如果您想在下一行继续您的语句,请在行末使用“ \”。 我在下面提到一个例子。
if (input_month == 'June') and (21 <= input_day <= 30) \
or (input_month == 'September') and (1 <= input_day <= 30):
print('Summer')
答案 2 :(得分:0)
这是您解决方案的正确且有效的代码。
input_month = input()
input_day = int(input())
# input reading for spring
if ((input_month == 'March') and (20 <= input_day <= 31)) or ((input_month == 'June') and (1 <= input_day <= 20)):
print('Spring')
# input reading for summer
elif (input_month == 'June') and (21 <= input_day <= 30) or (input_month == 'September') and (1 <= input_day <= 30):
print('Summer')
# input reading for Autumn
elif (input_month == 'September') and (22 <= input_day <= 30) or (input_month == 'December') and (1 <= input_day <= 20):
print('Autumn')
# input reading for winter
elif (input_month == 'December') and (21 <= input_day <= 31) or (input_month == 'January') and (
1 <= input_day <= 31) or (input_month == 'February') and (1 <= input_day <= 28) or (
input_month == 'March') and (1 <= input_day <= 19):
print('Winter')