python输入月份判断天数用函数的方法_请教@Robin_TY老师,请问python使用字典编程判断第几天时,用天数作键的字典的函数式怎么写?谢谢...

这个是可以作业,最好能自己独立完成。以下实现方式可以作为参考

"""

作者: Robin_TY

功能: 判断某日期是第几天

日期: 19/01/2018

"""

import datetime

def main():

"""

主函数

"""

input_date_str = input('请输入日期(yyyy/mm/dd):')

input_date = datetime.datetime.strptime(input_date_str, format('%Y/%m/%d'))

year = input_date.year

month = input_date.month

day = input_date.day

day_month_dict = {28: [2], # 28天的月份

30: [4, 6, 9, 11], # 30天的月份

31: [1, 3, 5, 7, 8, 10, 12]} # 31天的月份

days = 0

for i in range(1, month):

for day_key, month_list in day_month_dict.items():

if i in month_list:

# 如果当前月在列表中

days += day_key

days += day

is_leap_year = False

if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):

is_leap_year = True

if is_leap_year and month > 2:

days += 1

print('这是第{}天。'.format(days))

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值