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

CS50P作业遇ValueError:输入9/8/1636时如何修复?

CS50P作业日期转换问题:输入9/8/1636触发ValueError

正在学习CS50P,完成作业时遇到问题:输入日期9/8/1636会触发ValueError错误。使用VS Code开发,附上作业题目与编写的Python代码,曾求助GPT-4未解决,盼得到帮助。

作业题目

在美国,日期通常采用月-日-年(MM/DD/YYYY)格式,也称为中序(middle-endian)格式,该设计存在缺陷:此类日期无法轻松排序(年份在末尾),且有歧义——例如9/8/1636可被解读为1636年9月8日或8月9日。

计算机通常遵循ISO 8601国际标准,规定日期格式为年-月-日(YYYY-MM-DD),年份4位,月、日各2位,不足补前导零。

请在outdated.py文件中实现程序:提示用户输入公元(anno Domini)日期,支持两种格式(如9/8/1636或September 8, 1636,后者月份需为给定英文列表内的名称),转换为YYYY-MM-DD格式输出;若输入无效则重新提示,默认每月最多31天,无需验证具体月份天数。

我的代码

month_list = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]
while True:
    try:
        date = input('Please enter a date in month-day-year order: ').replace(' ','/').replace(',','/')
        date_list = list(date.split('/'))
        month = date_list[0]
        day = date_list[1]
        year = date_list[2]

        if month in month_list:
            month = month_list.index(month) + 1
        elif month.isdigit() and 1<=int(month)<=12:
            month=int(month)
        else:
            print('Month should be a valid month name or a number between 1 and 12.')
            continue

        if 1<=int(day)<=31:
                print(f'{year}-{month:02d}-{day:02d}')
        else:
            print('Day should be between 1 and 31.')
            continue
    
    except ValueError:
        print('Date invalid, please enter again.')

问题原因与解决办法

错误原因

输入9/8/1636时,day变量是从字符串分割得到的字符串类型('8'),而在print(f'{year}-{month:02d}-{day:02d}')中使用了02d格式符——该格式符要求输入为整数类型,但字符串无法被该格式处理,因此触发ValueError。

修正方案

将day转换为整数类型即可解决问题,具体修改如下:

  1. 在分割日期后,将day转为整数:day = int(date_list[1])
  2. 调整后续的判断条件为直接使用整数day:if 1<=day<=31:

修正后的代码

month_list = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]
while True:
    try:
        date = input('Please enter a date in month-day-year order: ').replace(' ','/').replace(',','/')
        date_list = list(date.split('/'))
        month = date_list[0]
        # 将day转为整数
        day = int(date_list[1])
        year = date_list[2]

        if month in month_list:
            month = month_list.index(month) + 1
        elif month.isdigit() and 1<=int(month)<=12:
            month=int(month)
        else:
            print('Month should be a valid month name or a number between 1 and 12.')
            continue

        # 直接使用整数day判断
        if 1<=day<=31:
                print(f'{year}-{month:02d}-{day:02d}')
        else:
            print('Day should be between 1 and 31.')
            continue
    
    except ValueError:
        print('Date invalid, please enter again.')

内容的提问来源于stack exchange,提问作者Fibonacci's Kettle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:23:23