判断第几天0.1
案例描述
案例分析
元组
修改会报错
程序及结果
'''
作者:张梦
功能:输入日期,判断这一天是这一年的第几天
版本:1.0
日期:2018/12/16
'''
from datetime import datetime
'''主函数'''
def main():
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str,'%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
day_in_month_tup = (31,28,31,30,31,30,31,31,30,30,31)
days = sum(day_in_month_tup[: month - 1])+day
# 判断闰年
if(year % 400 == 0)or((year % 4 == 0)and(year % 100 != 0)):
if month > 2:
days += 1
print('这是第{}天'.format(days))
if __name__ == '__main__':
main()
程序模块化:
子程序
一般不写两个return,所以改成如下
主函数中:
结果
判断第几天0.2
案例描述
案例分析
可以无需判断是否是二月,是不是大于2月,天数要加一。这回只要如果是闰年的话,可以直接更改列表里的数字28改为29
列表与元组
程序及结果
'''
作者:张梦
功能:输入日期,判断这一天是这一年的第几天
版本:2.0
2.0增加功能:用列表替换元组
日期:2018/12/18
'''
from datetime import datetime
'''
判断year是否为闰年
是:返回true
否:返回false
'''
def is_leap_year(year):
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
'''
主函数
'''
def main():
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str,'%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 计算之前月份天数以及当前月份天数
day_in_month_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 30, 31]
if is_leap_year(year):
day_in_month_list[1] = 29
days = sum(day_in_month_list[: month - 1])+day
print('这是{}年的第{}天'.format(year, days))
if __name__ == '__main__':
main()
判断第几天0.3
案例描述
案例分析
集合
示例:
集合里面不能有重复的,所以l后面加上5和3,在集合里面也没用
程序及结果
'''
作者:张梦
功能:输入日期,判断这一天是这一年的第几天
版本:3.0
2.0增加功能:用列表替换元组
2.0增加功能:将月份划分为不同的集合再操作
日期:2018/12/18
'''
from datetime import datetime
'''
判断year是否为闰年
是:返回true
否:返回false
'''
def is_leap_year(year):
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
'''
主函数
'''
def main():
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str,'%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 包含30天 月份集合
_30_days_month_set = {4, 6, 9, 11} # 前面加_,因为数字不能打头
_31_days_month_set = {1, 3, 5, 7, 8, 10, 12}
# 初始化值
days = 0
days += day
for i in range(1, month):
if i in _30_days_month_set:
days += 30
elif i in _31_days_month_set:
days += 31
else:
days += 28
if is_leap_year(year) and month > 2:
days +=1
print('这是{}年的第{}天'.format(year, days))
if __name__ == '__main__':
main()
如果不加上面的初始天数,那么就会有下面的错误,i=1时,for循环不进入
正确结果:
判断第几天0.3
案例描述
案例分析
字典
例子:
出现的错误(从第二版开始,程序模块化后)
错误的子程序:
因为虽然修改了is_leap但是,没有return,所以没有传到主函数,导致结果没有修改
错误的结果:
正确的子程序:
正确的结果:
完整程序及结果
月份-天数 字典
'''
作者:张梦
功能:输入日期,判断这一天是这一年的第几天
版本:4.0
2.0增加功能:用列表替换元组
3.0增加功能:将月份划分为不同的集合再操作
4.0增加功能:将月份及其对应天数通过字典表示
日期:2018/12/22
'''
from datetime import datetime
'''
判断year是否为闰年
是:返回true
否:返回false
'''
def is_leap_year(year):
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
'''
主函数
'''
def main():
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str,'%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 月份-天数 字典
month_day_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}
# 初始化值
days = 0
days += day
for i in range(1, month):
days += month_day_dict[i]
if is_leap_year(year) and month > 2:
days += 1
print('这是{}年的第{}天'.format(year, days))
if __name__ == '__main__':
main()
天数-月份 字典
(和集合很像)
from datetime import datetime
'''
判断year是否为闰年
是:返回true
否:返回false
'''
def is_leap_year(year):
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
'''
主函数
'''
def main():
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str,'%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 天数-月份 字典
day_month_dict = {30: {4, 6, 9, 11},
31: {1, 3, 5, 7, 8, 10, 12}}
# 初始化值
days = 0
days += day
for i in range(1, month):
if i in day_month_dict.get(30):
days += 30
elif i in day_month_dict.get(31):
days += 31
else:
days += 28
if is_leap_year(year) and month > 2:
days += 1
print('这是{}年的第{}天'.format(year, days))
if __name__ == '__main__':
main()
结果:
课后练习
time — Time access and conversions — Python 3.7.2rc1 documentation https://docs.python.org/3/library/time.html#time.strftime