日期判断季节代码问题排查:无法识别9月31日为无效日期
问题:日期转季节代码的无效日期验证问题
我正在编写一段根据输入的月和日输出对应季节的代码,规则如下:
- 春季:3月20日 - 6月20日
- 夏季:6月21日 - 9月21日
- 秋季:9月22日 - 12月20日
- 冬季:12月21日 - 3月19日
示例输入:
April 11
示例输出:Spring
但当输入September和31时,代码输出Autumn而非Invalid,找不到问题所在,求帮忙。以下是我使用的代码:
# Gather month and day inputs month = input() day = int(input()) # Determine the start and end dates for each season spring_start = (3, 20) spring_end = (6, 20) summer_start = (6, 21) summer_end = (9, 21) autumn_start = (9, 22) autumn_end = (12, 20) winter_start = (12, 21) winter_end = (3, 19) # Create a tuple 'months_list' months_list = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') # Validate the input month if month not in months_list: print("Invalid") else: # Validate the input day if day < 1 or day > 31: print("Invalid") else: # Convert the month to its matching numeric value month_to_num = { "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12 } numeric_month = month_to_num[month] # Determine the season based on the input date if (numeric_month == 3 and day >= 20) or (numeric_month > 3 and numeric_month < 6) or (numeric_month == 6 and day <= 20): print("Spring") elif (numeric_month == 6 and day >= 21) or (numeric_month > 6 and numeric_month < 9) or (numeric_month == 9 and day <= 21): print("Summer") elif (numeric_month == 9 and day >= 22) or (numeric_month > 9 and numeric_month < 12) or (numeric_month == 12 and day <= 20): print("Autumn") else: print("Winter")
问题原因
你的日期校验只判断了天数是否在1-31之间,但没考虑不同月份的实际最大天数。比如9月只有30天,输入31号属于无效日期,但代码没拦截,直接进入季节判断逻辑,所以输出了Autumn。
解决方案
给每个月份设置对应的最大天数,完善日期校验逻辑。可以用字典存储各月份的最大天数,然后根据输入的月份获取对应的最大值进行判断。
修改后的完整代码如下:
# Gather month and day inputs month = input().strip() try: day = int(input().strip()) except ValueError: print("Invalid") exit() # 定义各月份的最大天数(暂不考虑闰年) month_max_days = { "January": 31, "February": 28, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31 } # Validate the input month if month not in month_max_days: print("Invalid") else: # Validate the input day if day < 1 or day > month_max_days[month]: print("Invalid") else: # Convert the month to its matching numeric value month_to_num = { "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12 } numeric_month = month_to_num[month] # Determine the season based on the input date if (numeric_month == 3 and day >= 20) or (3 < numeric_month < 6) or (numeric_month == 6 and day <= 20): print("Spring") elif (numeric_month == 6 and day >= 21) or (6 < numeric_month < 9) or (numeric_month == 9 and day <= 21): print("Summer") elif (numeric_month == 9 and day >= 22) or (9 < numeric_month < 12) or (numeric_month == 12 and day <= 20): print("Autumn") else: print("Winter")
额外优化点
- 增加了输入格式校验:如果输入的day不是整数,直接输出Invalid并退出
- 合并了月份列表和最大天数字典,减少冗余定义
- 简化了季节判断的条件表达式,可读性更强
内容的提问来源于stack exchange,提问作者superior_light
相关产品推荐
相关产品推荐

