CS50 Python作业outdated.py代码求助:无效输入未被拦截
CS50 Python 作业三:outdated.py 调试求助
我正在完成CS50 Python课程的Problem Set 3作业,目标是实现outdated.py程序:接收两种格式的公元日期(如9/8/1636或September 8, 1636),转换为YYYY-MM-DD格式输出,无效输入需重新提示。
目前我的代码如下:
months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] def get_date(): while True: date = input("Date: ").title().strip() try: month, day, year = date.replace("/", " ").replace(",", " ").split() if month in months and 1 <= int(day) <= 31: month_number = months.index(month) + 1 print(year, str(month_number).zfill(2), day.zfill(2), sep="-") break if 1 <= int(month) <= 12 and 1 <= int(day) <= 31: print(year, month.zfill(2), day.zfill(2), sep="-") break except ValueError: pass get_date()
我希望自行排查问题而非直接复制答案,但有两个测试用例未通过:
- 输入
October/9/1701未被拦截,程序未按要求重新提示 - 输入
September 8 1636未被拦截,程序未按要求重新提示
测试反馈详情:
:) outdated.py exists :) input of 9/8/1636 outputs 1636-09-08 :) input of September 8, 1636 outputs 1636-09-08 :) input of 10/9/1701 outputs 1701-10-09 :) input of October 9, 1701 outputs 1701-10-09 :) input of " 9/8/1636 " outputs 1636-09-08 :) input of 23/6/1912 results in reprompt :) input of 10 December, 1815 results in reprompt :( input of October/9/1701 results in reprompt expected program to reject input, but it did not :) input of 1/50/2000 results in reprompt :) input of December 80, 1980 results in reprompt :( input of September 8 1636 results in reprompt expected program to reject input, but it did not
内容的提问来源于stack exchange,提问作者kevzoz
相关产品推荐
相关产品推荐

