Python:检查日期是否正确

PYTHON


您好,check_date 代码返回 False - 编码有什么问题?第一个函数检查输入的月份中有多少天,第二个函数检查日期格式是否正确。


'''

def days_in_month(month):

if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:

return month == 31

    else:

        return 30

'''



'''


    def check_date(date):

    day = int(date[0:2])

    month = int(date[2:4])

    year = date[4:6]

    

    if ( (day<=days_in_month(month)) and (0<month<13) ):

        return True

    else:

        return False

'''    

    

print(check_date('011297'))


开满天机
浏览 187回答 1
1回答

陪伴而非守候

您的错误在第 3 行:def days_in_month(month):&nbsp; &nbsp; if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:&nbsp; &nbsp; &nbsp; &nbsp; return 31&nbsp; &nbsp; else: return 30def check_date(date):&nbsp; &nbsp; day = int(date[0:2])&nbsp; &nbsp; month = int(date[2:4])&nbsp; &nbsp; year = date[4:6]&nbsp; &nbsp; if ( (day<=days_in_month(month)) and (0<month<13) ):&nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; return Falseprint(check_date('011297'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python