链接索引打印空白python



我找不到相关的线程。我在四季实验室工作。我需要能够花一个月和一天的时间来打印这个季节。对于非日历日和奇怪的输入,它需要打印invalid。我决定使用列表,但它们一直打印空白输出。我不确定我的代码在哪里结束。


input_month = str(input()).lower() # make user input a lower case string
input_month = input_month.replace(' ', '') # remove spaces in user input
input_day = int(input()) # make user input a number
# list of seasons
spring = ['march', 'april', 'may', 'june']
summer = ['june', 'july', 'august', 'september']
autumn = ['september', 'october', 'november', 'december']
winter = ['december', 'january', 'february', 'march']
season = ['Spring', 'Summer', 'Autumn', 'Winter']
if input_month in spring: # "march" or 'april' or 'may' or 'june':
if (input_month == spring[0]) and (31 >= input_day > 19):
season = season[0]
print(season)
elif (input_month == spring[0]) and (1 <= input_day < 19):
season = season[3]
print(season)
elif (input_month == spring[3]) and (1 <= input_day < 21):
season = season[0]
print(season)
elif (input_month == spring[3]) and (31 <= input_day > 21):
season = season[1]
print(season)
elif (input_month == spring[1] or input_month == spring[2]) and (1 <= input_day <= 31):
season = season[0]
print(season)
else:
print('Invalid')


我收到这个输出:

输入图片描述


当我放置一个断点然后调试并跳过时,输入march 19,只是跳过了elif语句:

elif (input_month == spring[0]) and (1 <= input_day < 19):
season = season[3]
print(season)


我真的很困惑,为什么当我使用计算器的东西(评估在调试?,它表示语句:

(input_month == spring[0]) and (1 <= input_day < 19)

,为假

我的解释(input_month是march, input_day是19):

如果input_month与春季列表中的索引0相同,即'march',且input_day在1(含1)之间且小于19…我认为这是真的,所以它应该在elif语句中被删除。

输入图片描述

我完全困惑在哪里我的逻辑是错误的,我找不到任何东西,除了'和'可能只是停止我的代码。但它还在继续。我的第一门课才上了第三周,我甚至不确定我是否在搜索正确的术语。嗯,我很确定我根本没有在搜索正确的术语。如果有任何帮助,以及对研究术语的任何建议,我们将不胜感激。

Thanks in advance jam.

添加* *谢谢你的帮助。我差点就猜出来了,原来是我在用<和比;还有我使用"and"这个词的方式,还有我把"December"这样的东西放在两个列表里,等等。现在我只是有-1的问题,而不是打印无效。>

elif input_month in autumn:
if input_month == autumn[1]:
if 1 <= input_day <= 31:
season = season[3]
print(season)
else:
print('Invalid')
elif input_month == autumn[2]:
if 1 <= input_day <= 30:
season = season[2]
print(season)
else:
print('Invalid')
elif input_month == autumn[3]:
if 1 <= input_day <= 20:
season = season[2]
print(season)
elif 21 >= input_day <= 31:
season = season[3]
print(season)
else:
print('Invalid')

输入月份" april ";不等于spring列表/数组中的任何内容。格式有问题

最新更新