首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第6行'if/elif statements‘和' and /or statements’出现语法错误

第6行'if/elif statements‘和' and /or statements’出现语法错误
EN

Stack Overflow用户
提问于 2020-05-30 02:47:44
回答 3查看 65关注 0票数 1

当我尝试运行这段代码时,我不断地在第6行得到一个语法错误。我已经尝试过处理缩进,并浏览了这个网站上的相关问题,但都无济于事。任何帮助都将不胜感激!谢谢!

代码语言:javascript
复制
#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‘,这在过去意味着我没有定义变量或者没有正确地写入变量。然而,情况似乎并非如此。

感谢大家的帮助!!

以下是更新后的工作代码,包括所有月份:

代码语言:javascript
复制
#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')
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-30 02:54:41

这是一个语法正确的代码(带反斜杠):

代码语言:javascript
复制
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风格和紧凑性。例如,如下所示:

代码语言:javascript
复制
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

或者(效率较低的方式)如下所示:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2020-05-30 02:54:53

您首先会忘记在每个if和else语句的末尾使用分号。如果你想在下一行继续你的语句,在行尾使用"\“。我在下面举一个例子。

代码语言:javascript
复制
if (input_month == 'June') and (21 <= input_day <= 30) \
  or (input_month == 'September') and (1 <= input_day <= 30):
print('Summer')
票数 0
EN

Stack Overflow用户

发布于 2020-05-30 02:57:56

这是您的解决方案的正确和有效的代码。

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62092773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档