学校作业5_2字符串_日期分析处理(头哥作业[Python])

  本系列文章为同学困扰的python做出作业答案汇总,答案皆为本学生自己边学边做汇总上传的,编排不易,希望大家喜欢。

本作业所有内容均在github上有留存,每次作业github上会最先发布,CSDN上需要编辑将会慢一两天,着急完成作业的同学可以前往github抢先学习:Opve2/homework: 这个作业 (github.com)

1_ 判断闰年

def leap(current_date):
    """接收一个用8个字符表示日期的字符串为参数,判断这个日期中的年份是否为闰年
    返回值为布尔型。
    @参数 current_date:表示日期,字符串类型
    闰年的判定方法是:能被400整除或能被4整除且同时不能被100整除的是闰年。
    # >>> test_date = '20000426'
    # >>> leap(test_date)
    # True
    # >>> test_date = '19000426'
    # >>> leap(test_date)
    # False
    # >>> test_date = '19990426'
    # >>> leap(test_date)
    # False
    """
    ########## Begin ##########
    if int(current_date) % 400 == 0 or int(current_date) % 4 == 0 and int(current_date) %100 != 0 :
        return True
    else:
        return False

    ########## End ##########


if __name__ == '__main__':
    CurrentDate = input()
    if leap(CurrentDate[:4]):
        print(f'{CurrentDate[:4]}年是闰年')
    else:
        print(f'{CurrentDate[:4]}年不是闰年')

2_输出当前月份共有多少天

def days_of_month(current_date):
    """接收一个用8个字符表示日期的字符串为参数,计算这个日期中的月份有多少天?返回值为整型,
    表示当前月份天数。
    @参数 current_date:表示日期,字符串类型
    # >>> test_date = '20000826'
    # >>> days_of_month(test_date)
    # 31
    # >>> test_date = '19000226'
    # >>> days_of_month(test_date)
    # 28
    # >>> test_date = '19800226'
    # >>> days_of_month(test_date)
    # 29
    # >>> test_date = '19990430'
    # >>> days_of_month(test_date)
    # 30
    """
    ########## Begin ##########
    moths_list = [31,28,31,30,31,30,31,31,30,31,30,31]
    if leap(current_date):
        moths_list[1] =29
    month = int(CurrentDate[4:6])
    return moths_list[month-1]
    
    ########## End ##########

def leap(current_date):
    year = int(current_date[:4])
    if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
        return True
    else:
        return False

if __name__ == '__main__':
    CurrentDate = input()
    days = days_of_month(CurrentDate)
    print(f'{CurrentDate[:4]}年{int(CurrentDate[4:6])}月有{days}天')

3_日期转换格式

def separate_date(current_date, symbol):
    """接收一个用8个字符表示日期的字符串和一个符号为参数,返回用该符号分隔的日期,字符串类型。
    @参数 current_date:表示日期,字符串类型
    @参数 symbol:分隔符号,字符串类型
    例如传入'20201031'和"/",返回字符串'2020/09/09'
    # >>> test_date = '20000826'
    # >>> sep = '/'
    # >>> separate_date(test_date, sep)
    # '2000/08/26'
    # >>> test_date = '19801226'
    # >>> sep = '-'
    # >>> separate_date(test_date, sep)
    # '1980-12-26'
    """
    ########## Begin ##########
    a = current_date[:4] + symbol + current_date[4:6] + symbol +current_date[6:8]
    return a
    ########## End ##########



if __name__ == '__main__':
    CurrentDate = input()
    sign = input()
    print(separate_date(CurrentDate, sign))

4_判断日期合法性 

def legal_judge(current_date):
    """接收一个用8个字符表示日期的字符串为参数,判定日期的合法性,返回值为布尔型。
    1,3,5,7,8,10,12月各31天,4,6,9,11各30天,闰年2月29天,平年2月28天。
    @参数 current_date:表示日期,字符串类型
    # >>> test_date = '20000426'
    # >>> legal_judge(test_date)
    # True
    # >>> test_date = '19000229'
    # >>> legal_judge(test_date)
    # False
    # >>> test_date = '19990431'
    # >>> legal_judge(test_date)
    # False
    """
    ########## Begin ##########
    moths_list = [31,28,31,30,31,30,31,31,30,31,30,31]
    if leap(current_date):
        moths_list[1] =29
    month = int(CurrentDate[4:6])
    if moths_list[month-1] >= int(current_date[6:8]):
        return True
    else:
        return False
    ########## End ##########

def leap(current_date):
    year = int(current_date[:4])
    if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
        return True
    else:
        return False

if __name__ == '__main__':
    CurrentDate = input()
    if legal_judge(CurrentDate):
        print(f'{CurrentDate}是合法日期')
    else:
        print(f'{CurrentDate}是非法日期')

5_月份的英文名称

def name_of_month(current_date):
    """接收一个用8个字符表示日期的字符串为参数,返回这个月份对应的英文单词及其缩写形式。
    @参数 current_date:表示日期,字符串类型
    例如:current_date为20201031,返回值为'October','Oct.'
    # >>> test_date = '20000826'
    # >>> name_of_month(test_date)
    # ('August', 'Aug.')
    # >>> test_date = '19000226'
    # >>> name_of_month(test_date)
    # ('February', 'Feb.')
    # >>> test_date = '19801226'
    # >>> name_of_month(test_date)
    # ('December', 'Dec.')
    # >>> test_date = '19990430'
    # >>> name_of_month(test_date)
    # ('April', 'Apr.')
    """
    # 日期的英文全称:['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December']
    # 日期的英文缩写:September为前四位字母加点,其余月份均为前三位字母加点,如:'Sept.','Jan.'
    ########## Begin ##########
    moths_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December'] 
    month = int(current_date[4:6]) -1
    English_Month = moths_list[month] 
    if month == 8:
        abbreviation = English_Month[:4] +'.'
    else:
        abbreviation = English_Month[:3] +'.'
    return English_Month ,  abbreviation
    ########## End ##########



if __name__ == '__main__':
    CurrentDate = input()
    monthName, monthAbbr = name_of_month(CurrentDate)
    print(f'{int(CurrentDate[4:6])}月英文是{monthName},缩写为{monthAbbr}')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值