Python练习——输入某年月日,判断是一年中第几天?(条件循环、输入、求和)
题目:输入某年某月某日,判断这一天是这一年的第几天?
#先输入平年与闰年在月份上的差异
#创建一个输入器:用于输入年、月、日
#需要规避掉基础输入错误,然后求解在两种条件下的天数
run_dict={1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
ping_dict={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
y=int(input("请输入年份:"))
m=int(input("请输入月份:"))
d=int(input("请输入日期:"))
sum=0
if 0<m<=12 and 0<d<=31:
if(y%400==0)or((y%400==0))and (y%100!=0):
for i in range(1,m):
sum=sum+run_dict[i]
print("这一天是{}的第{}天".format(y,sum+d))
else:
for i in range(1,m):
sum=sum+ping_dict[i]
print("这一天是{}的第{}天".format(y,sum+d))
else:
print("Data Error")
output:
请输入年份:2019
请输入月份:6
请输入日期:27
这一天是2019的第178天
方法2:
'''Python 练习实例4
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然
后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:'''
year=int(input())
month=int(input())
day=int(input())
list=[31,60,91,121,152,182,213,244,274,305,335,366]
if month==1:
count=day
if (year%4==0 and year%100!=0)or year%400==0:
if month>1 and month<13:
count=list[month-2]+day
else:
if month>1 and month<13:
count=list[month-2]+day-1
print("第"+str(count)+"天")