HJ73-日期转换成天数-遇到的一些小bug和自带函数记录
˚∆˚DEBUG明明同样的数据自测是正确的,但是提交却不输出,原因:
测试用例有多组,注意循环输入
while True:
try:
…
except:
break
˚∆˚DEBUG保存并调试如果遇到问题(比如不输出)有可能是因为代码有bug,比如读入不对,或者变量没有定义,list没有定义大小,或者tab没有正确等
˚∆˚DEBUG最开始一直自测不正确,原因:计算year是否为闰年,应该取余%,我用的全是//,一直没有觉察到,知道我在terminal测试了一下if语句,才发现这个bug
˚∆˚闰年:(1) %4==0 and % 100 != 0 (2) %400 == 0
˚∆˚自带日期转换函数:
import datatime
datetime.datetime(*map(int, input().split())).strftime("%j").lstrip("0")
while True: try: year, month, day = map(int, input().split()) ans = 0 if ((year%4 == 0 and year%100 != 0) or (year%400==0)): month_list = [31, 29, 31, 30, 31, 30,31,31,30,31,30,31] elif ((year %400 != 0 and year %100 ==0) or (year%4 != 0)): month_list[1] = 28 #不需要重新赋值,只修改一下就可 ans += day for i in range(month-1): ans += month_list[i] print(ans) except: break
while True: try: year, month, day = map(int, input().split()) m = 1 ans = 0 while m < month: if m in [1, 3, 5, 7, 8, 10, 12]: ans += 31 elif m in [4, 6, 9, 11]: ans += 30 elif m == 2 and ((year%4 == 0 and year%100 != 0) or (year%400==0)): ans += 29 elif m == 2 and ((year %400 != 0 and year %100 ==0) or (year%4 != 0)): ans += 28 m+=1 #终止条件是m=month ans += day print(ans) except: break