代码经过测试,个人感觉没有什么问题,但由于个人的经验不足以及眼光不够,希望各位指正程序中的不足之处。
附上代码
def check():
'''
输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天
'''
print('输入一个日期,格式为(xxxx-xx-xx):', end='')
data = input()
if '-' not in data or data.count('-') != 2:
print('请按照正确格式输入年月日')
return check()
else:
str1 = data.split('-')
year = int(str1[0]) # 年
month = int(str1[1]) # 月
day = int(str1[2]) # 日
if month < 1 or month > 13:
print('月输入错误,请重新输入')
return check()
elif month in day31: # 月的天数为31天
if day > 31 or day == 0:
print('日输入错误,该月只有31天,请重新输入')
return check()
else:
if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0: # 判断是否为闰年
print(year366[month - 1] + day)
else:
print(year365[month - 1] + day)
elif month in day30: # 月的天数为30天
if day > 30 or day == 0:
print('日输入错误,该月只有30天,请重新输入')
return check()
else:
if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0: # 判断是否为闰年
print(year366[month - 1] + day)
else:
print(year365[month - 1] + day)
elif month == 2:
if ((day <= 28 or (year % 4 == 0 and year % 100 != 0 and day < 29) or (year % 400 == 0 and day <= 29))):
if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0: # 判断是否为闰年
print(year366[month - 1]+day)
else:
print(year365[month - 1]+day)
else:
print('日输入错误,2月最多29天,或者该年不是闰年,请重新输入')
return check()
if __name__ == '__main__':
day31 = [1, 3, 5, 7, 8, 10, 12] #天数为31天的月份
day30 = [4, 6, 9, 11] #天数为30天的月份
year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365] #平年每个月结束时的天数 365天
year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] #闰年每个月结束时的天数 366天(2月多一天)
check()
更新一个小错误,判断闰年的时候都是闰年,现已经更正。
def check():
'''
输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天
'''
print('输入一个日期,格式为(xxxx-xx-xx):', end='')
data = input()
if '-' not in data or data.count('-') != 2:
print('请按照正确格式输入年月日')
return check()
else:
str1 = data.split('-')
year = int(str1[0]) # 年
month = int(str1[1]) # 月
day = int(str1[2]) # 日
if month < 1 or month > 13:
print('月输入错误,请重新输入')
return check()
elif month in day31: # 月的天数为31天
if day > 31 or day == 0:
print('日输入错误,该月只有31天,请重新输入')
return check()
else:
if year % 4 == 0 : # 判断是否为闰年
print(year366[month - 1] + day)
else:
print(year365[month - 1] + day)
elif month in day30: # 月的天数为30天
if day > 30 or day == 0:
print('日输入错误,该月只有30天,请重新输入')
return check()
else:
if year % 4 == 0 : # 判断是否为闰年
print(year366[month - 1] + day)
else:
print(year365[month - 1] + day)
elif month == 2:
if ((day <= 28 or (year % 4 != 0 and day < 29) or (year % 4 == 0 and day <= 29))):
if year % 4 == 0 : # 判断是否为闰年
print(year366[month - 1]+day)
else:
print(year365[month - 1]+day)
else:
print('日输入错误,2月最多29天,或者该年不是闰年,请重新输入')
return check()
if __name__ == '__main__':
day31 = [1, 3, 5, 7, 8, 10, 12] #天数为31天的月份
day30 = [4, 6, 9, 11] #天数为30天的月份
year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365] #平年每个月结束时的天数 365天
year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] #闰年每个月结束时的天数 366天(2月多一天)
check()