【Python 练习实例4】输入某年某月某日,判断这一天是这一年的第几天?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
time = input('Pls input the date(YYYYMMDD):') year = int(time[0:4]) mon = int(time[4:6]) date = int(time[6:]) mon_date = [31,28,31,30,31,30,31,31,30,31,30,31] count = 0 if str(year)[-2:] == '00' and year % 400 ==0: mon_date[1] = 29 elif year % 4 ==0: mon_date[1] = 29 if date > mon_date[mon-1] or mon > 12: print('Date Error.') for m in range(mon-1): count += mon_date[m] count = count + date print(count) |
输出:
1 2 |
Pls input the date(YYYYMMDD):20000301 61 |
1 2 |
Pls input the date(YYYYMMDD):20000201 32 |
1 2 |
Pls input the date(YYYYMMDD):20000230 Date Error |