python练习任意输入年、月、日,输出这一天是这一年的第几天 提示:考虑是否是闰年

python练习(为新入坑5天新手写的:)
任意输入年、月、日,输出这一天是这一年的第几天 提示:考虑是否是闰年写

year=int(input("请输入年份:"))
month=int(input("请输入月份:"))
day=int(input("请输入日期:"))
#判断是不是闰年 闰年2月28天
#不是就29天
run_day=[31,29,31,30,31,30,31,31,30,31,30,31]
norun_day=[31,28,31,30,31,30,31,31,30,31,30,31]
# 定义环境变量x
x=0
# 去除错误的月份日期
if (month,day)==(2,30) or (month,day)==(2,31) or (month,day)==(4,31)or (month,day)==(6,31)or (month,day)==(9,31)or (month,day)==(11,31):
    print("请输入正确的月份或日期")
# 接下来输入的就是正确的了
else:
    # 继续去除错误的日期
    if day<=31 or month<=12:
        if year%4==0:
	        for i in range(0,month-1):
		        x+=run_day[i]
        else:
            for i in range(0, month - 1):
                x += norun_day[i]
        print("这是第%d天" %(x+day))
    else:
        print("请输入正确的月份或日期")

这份代码去除了2/30 2/31 4/31等错误日期,但是仍然没有限定输入的是数字,如果输入的是文字或者特殊字符会之间报错,后面学习之后再更改。

你可能感兴趣的:(python)