小肥羊要进步

Practice_Test

输入某月某日,判断这一天是一年的第几天?

利用Python语言,采用字典的方式对应月份和天数,采用选择结构和循环结构解决如下问题:输入某月某日,判断这一天是一年的第几天?输出“这是年度第XX天”,如若日期输入错误,则输出“error”。具体要求如下:

(1)创建python.py新文件并保存;

(2)采用字典的方式对应月份和天数;

(3)依次弹出对话“请输入月份:”和“请输入日期:”

(4)采用条件判断结构区分所输入日期是否正确,如果错误,输出“error”;

(5)如若输入日期正确,采用循环结构计算天数,输出“这是年度第XX天”;

y=eval(input('请输入年份:'))
m=eval(input('请输入月份:'))
d=eval(input('请输入日期:'))

flag=False #初始为False 平年 True为闰年


#判断是否为闰年
if y<9999 and m<13 and d<32:
if( y%400==0) :
flag=True
elif (y%100!=0 and y%4==0):
flag=True
else:
flag=False
else:
print('输入错误,请重新输入年月日!')

if flag==True :
ms = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
elif flag == False:
ms = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
print('日期输入错误!')

day=0
for i in range(0, m - 1):
day += ms[i] # day=day+ms[i]
day += d # day=day+d
print('是该年份的第%d天' % day)


way2

#题目:输入某年某月某日,判断这一天是这一年的第几天?
day=input('请输入年月日(格式举例:2000-01-05):')
year=int(day[:4])#将年份截取
month=int(day[5:7])#截取月份
sun=int(day[8:10])#截取日
print(year,month,sun)
t_run=[31,29,31,30,31,30,31,31,30,31,30,31]#闰年每个月天数
f_run=[31,28,31,30,31,30,31,31,30,31,30,31]#平年每个月的天数
tall_day=0#设总天数初始值为0
if year%4==0 and year%100!=0 or year%400==0:#判断闰年的条件
for i in range(month-1):#本月之前的所有月份天数循环求和
tall_day +=t_run[i]
else:
for i in range(month-1):
tall_day +=f_run[i]
print('您输出的是{}年的第{}天'.format(year,tall_day+sun))#之前所有月份加上本月的日数就是今年的多少天



2.冒泡排序
templist=[2,1,3,6,4,3,2,1]
print('before sort: ')
print(templist)

for i in range(0,len(templist)-1):
for j in range(0,len(templist)-1-i):
if templist[j]>templist[j+1]:
temp =templist[j+1]
templist[j+1]=templist[j]
templist[j]=temp
print("after order:")
print(templist)

#way2
templist=[4,1,2,7,5,3]
print('排序前:')
print(templist)

for j in range(0,len(templist)-1):
for i in range(0,len(templist)-1-j):
if templist[i]>templist[i+1]:
temp=templist[i+1]
templist[i+1]=templist[i]
templist[i]=temp

print('排序后:')
print(templist)

未完待续。。。。。持续更新

posted on 2020-09-20 13:12  小肥羊要进步  阅读(216)  评论(0编辑  收藏  举报

导航