当我尝试运行这段代码时,我不断地在第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 error‘,这在过去意味着我没有定义变量或者没有正确地写入变量。然而,情况似乎并非如此。
感谢大家的帮助!!
以下是更新后的工作代码,包括所有月份:
#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')发布于 2020-05-30 02:54:41
这是一个语法正确的代码(带反斜杠):
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发布于 2020-05-30 02:54:53
您首先会忘记在每个if和else语句的末尾使用分号。如果你想在下一行继续你的语句,在行尾使用"\“。我在下面举一个例子。
if (input_month == 'June') and (21 <= input_day <= 30) \
or (input_month == 'September') and (1 <= input_day <= 30):
print('Summer')发布于 2020-05-30 02:57:56
这是您的解决方案的正确和有效的代码。
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')https://stackoverflow.com/questions/62092773
复制相似问题