You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

日期天数计算器代码求助:输入验证功能失效排查

代码错误排查与修复

1. 语法错误:布尔值大小写错误

在month_date_valid函数中,返回的false是小写,Python中布尔值必须大写为False,否则会抛出NameError。
修复:

def month_date_valid(month):
    if month > 0 and month <= 12: 
        return True
    else: 
        return False  # 改为大写False

2. 逻辑错误:变量未正确累加

get_day_count函数中,count+31和count+30仅做了计算,没有将结果赋值回count,导致累加失效,最终天数计算错误。
修复:

if i in list1:
    count += 31  # 改为赋值式累加
elif i in list2: 
    count += 30  # 改为赋值式累加

3. 逻辑错误:for循环的else块误用

原代码中for i in range(1, month):的else块属于循环附属逻辑(仅当循环正常结束时执行),而非判断月份的分支逻辑,这会导致每次循环结束后额外加28天,完全不符合需求。正确逻辑应为:对每个遍历到的月份,判断其属于31天、30天还是2月,分别累加对应天数。
修复:

def get_day_count(month, days):
    list1 = [1,3,5,7,8,10,12]  # 修正31天月份列表(原漏了12,错写11)
    list2 = [4,6,9,11]         # 修正30天月份列表(原包含2,且漏了11)
    count = 0 
    for i in range(1, month):
        if i in list1:
            count += 31
        elif i in list2: 
            count += 30 
        else:  # 仅当i是2月时执行
            count += 28
    return count + days

4. 数据错误:月份列表内容错误

  • 原list1(31天月份)写了11,实际11月是30天,应替换为12(12月是31天)
  • 原list2包含2(2月是28天),且遗漏了11(11月是30天),需修正列表内容

5. 循环范围错误:31天月份的日期遍历范围不足

全局循环中,31天的月份(如1月)使用range(1, 30),只能遍历到29号,应改为range(1, 32)才能覆盖1-31号。
修复:

if i in list1:
    for j in range(1, 32):  # 改为32,遍历1-31
        answer = get_day_count(i,j)
        print((i, j), 'is', answer)

6. 未调用主函数,交互功能无法触发

代码定义了main函数,但未执行main(),导致程序运行时只会执行前面的全局循环打印,不会进入用户输入的交互逻辑。
修复:
在代码末尾添加:

if __name__ == "__main__":
    main()

7. 缺失日期有效性验证

main函数仅验证了月份的合法性,未检查日期是否符合对应月份的天数(如3月32号、2月30号等非法日期),需补充验证逻辑。
示例修复(在main函数中):

def main():
    month = int(input('Enter the month of the year: '))
    day = int(input('Enter the day of the year: '))
    if not month_date_valid(month):
        print('Month is invalid, try again')
        return
    # 验证日期有效性
    max_day = 28
    if month in [1,3,5,7,8,10,12]:
        max_day = 31
    elif month in [4,6,9,11]:
        max_day = 30
    if day < 1 or day > max_day:
        print('Day is invalid, try again')
        return
    # 计算并输出结果
    total_days = get_day_count(month, day)
    print(f"{month}/{day} is the {total_days}th day of the year")

修正后的完整代码

def month_date_valid(month):
    if month > 0 and month <= 12: 
        return True
    else: 
        return False

def get_day_count(month, days):
    list1 = [1,3,5,7,8,10,12]
    list2 = [4,6,9,11]
    count = 0 
    for i in range(1, month):
        if i in list1:
            count += 31
        elif i in list2: 
            count += 30 
        else:
            count += 28
    return count + days

# 可选:打印所有日期的天数(测试用)
list1 = [1,3,5,7,8,10,12]
list2 = [4,6,9,11]
for i in range(1,13):
    if i in list1:
        for j in range(1, 32):
            answer = get_day_count(i,j)
            print((i, j), 'is', answer)
    elif i in list2:
        for j in range(1, 31): 
            answer = get_day_count(i, j)
            print((i, j), 'is', answer)
    else: 
        for j in range(1,29):
            answer = get_day_count(i, j)
            print((i, j), 'is', answer) 

def main():
    month = int(input('Enter the month of the year: '))
    day = int(input('Enter the day of the year: '))
    if not month_date_valid(month):
        print('Month is invalid, try again')
        return
    # 验证日期合法性
    max_day = 28
    if month in [1,3,5,7,8,10,12]:
        max_day = 31
    elif month in [4,6,9,11]:
        max_day = 30
    if day < 1 or day > max_day:
        print('Day is invalid, try again')
        return
    total_days = get_day_count(month, day)
    print(f"{month}/{day} is the {total_days}th day of the year")

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者riverride

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 07:20:12