python - 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')
解决方案
推荐阅读
- python - 对象没有属性“通道”
- javascript - onChange vs onInput?
- python - 如何交替使用 glob.glob("*PDF) 和 os.listdr("./directory")?
- c# - 用 ASP.NET MVC 5 中的值填充 PDF 字段
- r - 基于在没有 for() 循环的情况下递归调用列的先前元素,在 R 中生成一个新的变量/列?
- c# - 在 C# 中使用 Moq 模拟接口实现
- django - 如何正确使用 AJAX 和 DJANGO?
- c# - 每转一圈用 NAudio 播放波形
- kotlin - 除了 Kotlin 中的最后一个之外,对列表中的元素求和的最干净的方法是什么?
- docker - 通过 no-sandbox 但仍然出现错误“不支持以 root 身份运行而没有 --no-sandbox。”