zoukankan      html  css  js  c++  java
  • 订购一个月产品,计算多久后到期

    python 2.7.14
    计算订购一个月产品的到期日期
    【思考过程及实现思路】
    首先判断是否闰年,这样发生在2月份产品订购可以很好的处理边界情况。
    默认“订购一个月”为固定30天
    接下来就是加上这30天,判断月份、年份是否进位。

    另外还给出了默认自然月的订购情况,在注释里,逻辑相对更简单。
    测试的话直接运行、输入对应日期即可。

    
    # coding: utf-8
    
    def getExpirationDate(year, month, day):
        # 判断闰年及对应每月天数
        if (year%4==0 and year%100!=0) or year%400==0:
            days_per_month=[31,29,31,30,31,30,31,31,30,31,30,31]
        else:
            days_per_month=[31,28,31,30,31,30,31,31,30,31,30,31]
    
        if day>days_per_month[month-1] or month>12:
            return "您输入的日期有误"
        
        #按固定30天算,可处理 2018 1 31 → 2018 3 2 这种边界情况
        day += 30
        while day>days_per_month[month-1]:
            day = day-days_per_month[month-1]
            month = month+1
            # 如果是下一年
            if month==13:
                year+=1
                month=1
        '''
        如果按自然月,以上则为:
        if day>days_per_month[month]:
            day = days_per_month[month]
    
        month = month+1
        if month==13:
            year+=1
            month=1
        '''
        return [year, month, day]
    
    
    print 'Please enter the date(exp:2018 1 31)'
    
    while True:
        try:
            year,month,day=map(int,raw_input('Date:').split())
            print getExpirationDate(year, month, day)
    
        except:
            break
    
    
  • 相关阅读:
    ubuntu系统安装微信小程序开发工具
    【工具】vscode-代码编辑器详解
    微信小程序开发
    webpack基本配置
    vue相关知识
    史上最强vue总结~万字长文---面试开发全靠它了
    ES6——字符串
    ES6——Proxy与Reflect
    ES6——Map与Set
    ES6——Symbol
  • 原文地址:https://www.cnblogs.com/aubucuo/p/ms1.html
Copyright © 2011-2022 走看看