Python:检查日期是否正确

Python:检查日期是否正确,python,Python,蟒蛇 您好,检查日期代码返回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 ''' '''

蟒蛇

您好,检查日期代码返回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'))
                
“”
定义月内天数(月):
如果月份==1或月份==3或月份==5或月份==7或月份==8或月份==10或月份==12:
返回月份==31
其他:
返回30
'''
'''
def检查日期(日期):
日期=整数(日期[0:2])
月份=整数(日期[2:4])
年份=日期[4:6]

如果((天您的错误在第3行:

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 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'))
def天数(月):
如果月份==1或月份==3或月份==5或月份==7或月份==8或月份==10或月份==12:
返回31
其他:返回30
def检查日期(日期):
日期=整数(日期[0:2])
月份=整数(日期[2:4])
年份=日期[4:6]

如果((day
day
是一个字符串。请尝试将其转换为整数,然后再将其与月(月)中的天(ofc)进行比较(这同样适用于月(ofc)(同样,对于月
中的天,您有点忘记了二月…)