题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
def feb_switch(x):
if x%4 == 0 :
return 1
else:
return 0
while 1:
date = input('Please input date in format YYYY.month.day\n')
x = date.split('.',3)
day = 0
big = [1, 3, 5, 7, 8, 10, 12]
feb = [28,29]
if feb_switch(int(x[0])) == 0 and int(x[1]) ==2 and int(x[2]) >28:
print("不是闰年,输入错误!!!")
elif feb_switch(int(x[0])) == 1 and int(x[1]) ==2 and int(x[2]) >29:
print("二月份哪有那么多天!!!")
elif int(x[1]) not in big and int(x[2]) >30:
print("当前月份只有30天!!!")
elif int(x[1]) in big and int(x[2]) >31:
print("当前月份只有31天!!!")
else:
for i in range(int(x[1]) - 1):
if i + 1 in big:
day += 31
elif i + 1 == 2:
day += feb[feb_switch(int(x[0]))]
else:
day += 30
day += int(x[2])
print(day)
其实应该把输入合法性单独列出来一个函数会整洁很多。。。有点懒了。。。
网友评论