标签:form pytho mon inpu span mat 转换 for 第几天
实现输入某年某月某日计算出这一天是该年第几天的方法
方法一:笨办法
1 list_day = [0,31,28,31,30,31,30,31,31,30,31,30,31] 2 year = int(input(‘请输入年‘)) 3 month = int(input(‘请输入月‘)) 4 day = int(input(‘请输入日‘)) 5 month_day = 0 6 if 0<month<=12: 7 if year % 400 == 0 or ((year % 4 ==0) and (year % 100 != 0)): 8 print(‘{}是闰年‘.format(year)) 9 list_day[1] = 29 10 for i in range(month-1): 11 month_day += list_day[i] 12 if day <= list_day[month]: 13 count_day = month_day + day 14 print(count_day) 15 else: 16 print(‘day error‘) 17 18 else: 19 for i in range(month): 20 month_day += list_day[i] 21 if day <= list_day[month]: 22 count_day = month_day + day 23 print(count_day) 24 else: 25 print(‘day error‘) 26 else: 27 print(‘month error‘)
方法二:简化方法一
1 list_day = [0,31,28,31,30,31,30,31,31,30,31,30,31] 2 year = int(input(‘请输入年‘)) 3 month = int(input(‘请输入月‘)) 4 day = int(input(‘请输入日‘)) 5 if 0< month <=12: 6 sum = list_day[month-1] 7 else: 8 print(‘data error‘) 9 sum += day 10 11 leap = 0 # 12 if year % 400 == 0 or ((year % 4 ==0) and (year % 100 != 0)): 13 leap = 1 14 if leap == 1 and month > 2: 15 sum += 1 16 print(sum)
方法三:使用模块time和datetime
1 import datetime 2 import time 3 def function2(year, month, day): # 直接使用Python内置模块datetime的格式转换功能得到结果 4 date = datetime.date(year, month, day) 5 return date.strftime(‘%j‘) 6 print(function2(2018,12,3))
练习四:日期计算(输入某年某月某日计算出这一天是该年第几天的方法)
标签:form pytho mon inpu span mat 转换 for 第几天
原文地址:https://www.cnblogs.com/pinpin/p/9858686.html