首页 > 解决方案 > 4 Seasons - 在 pyCharm 中不产生输出,但在其他 IDE 中显示无效

问题描述

我正在编写一个程序,它从用户那里获取一个月和一天的输入,然后确定一天属于四个季节中的哪一个。我们得到了下面的特定季节,用户需要输入月份和日期。

下面是我想出的。它可能可以简化......但是,我仍然想知道为什么在 PyCharm 中执行它时没有产生输出......当它在另一个 IDE 中运行时它输出“无效”。

样本输入:

March
25

我检查了用户输入以确保它与月份匹配,并且日期小于 31。(可能不是最佳解决方案)。我正在检查月份是否在季节范围内,然后检查日期是否在季节范围内。为了照顾不满足日期要求的月份,我检查月份是否大于和小于季节的开始和结束月份,这样可以忽略介于两者之间的月份的日期。否则打印无效。

请告知如何解决此问题。

from collections import namedtuple
season = namedtuple('Season', ['start_mo', 'start_mo_int', 'start_day', 'end_mo', 'end_mo_int', 'end_day'])
spring = season('March', 3, 20, 'June', 6, 20)
summer = season('June', 6, 21, 'September', 9, 21)
autumn = season('September', 9, 22, 'December', 12, 20)
winter = season('December', 12, 21, 'March', 3, 19)
months = {
    'January': 1,
    'February': 2,
    'March': 3,
    'April': 4,
    'May': 5,
    'June': 6,
    'July': 7,
    'August': 8,
    'September': 9,
    'October': 10,
    'November': 11,
    'December': 12
}
input_month = input()
input_day = int(input())
im = months.get(input_month, -1)
if input_month in months and (input_day <= 31):
    if (((im >= spring.start_mo_int) and (im <= spring.end_mo_int)) 
            and ((input_day >= spring.start_day) and (input_day <= spring.end_day))) \
        or ((months.get(input_month, -1) > spring.start_mo_int) 
            and (months.get(input_month) < spring.end_mo_int)):
        print('Spring')
    elif (((im >= summer.start_mo_int) and (im <= summer.end_mo_int)) 
          and ((input_day >= summer.start_day) and (input_day <= summer.end_day))) \
        or ((months.get(input_month, -1) > summer.start_mo_int) 
            and (months.get(input_month) < summer.end_mo_int)):
        print('Summer')
    elif (((im >= autumn.start_mo_int) and (im <= autumn.end_mo_int)) 
          and ((input_day >= autumn.start_day) and (input_day <= autumn.end_day))) \
        or ((months.get(input_month, -1) > autumn.start_mo_int) 
            and (months.get(input_month) < autumn.end_mo_int)):
        print('Autumn')
    elif (((im >= winter.start_mo_int) and (im <= winter.end_mo_int)) 
          and ((input_day >= winter.start_day) and (input_day <= winter.end_day))) \
        or ((months.get(input_month, -1) > winter.start_mo_int) 
            and (months.get(input_month) < winter.end_mo_int)):
        print('Winter')
else:
    print('Invalid')

标签: pythondictionarybranchnamedtupleswitching

解决方案


推荐阅读