Python 创建检查日期是否有效的函数

Python 创建检查日期是否有效的函数,python,Python,修订问题之一是创建一个函数,用于检查日期是否有效并返回布尔值。我们得到了函数的前两行,也是这样 编辑:我们不允许使用内置函数来完成所有工作,例如date.time month_names = ["January", "February", "March", "April", "May", "June", "July", "August",

修订问题之一是创建一个函数,用于检查日期是否有效并返回布尔值。我们得到了函数的前两行,也是这样

编辑:我们不允许使用内置函数来完成所有工作,例如date.time

month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
其中,月内天数列表包含各个月的最大天数

测试用例:

迄今为止的代码:

def is_a_valid_date(date):
    month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
    clean_date = date.split()
    clean_date[1:] = ["".join(clean_date[1:])]
    
    a = False
    b = False
    
    if clean_date[0] in month_names:
        a = True
        x = month_names.find(clean_date[0])
    else:
        a = a
    
    if clean_date[1].isdigit() == True and int(clean_date[1]) <= int(days_in_month[x]):
        b = True
    else:
        b = b

    if a == True and b == True:
        return True
    else:
        return False

我很难弄清楚如何创建一个条件来查看输入的日期编号是否。您可以将该条件简化为:

def is_a_valid_date(date):
    month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
                   "November", "December"]
    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    month, day = date.split(maxsplit=1)

    if day.isdigit():
        return int(day) <= days_in_month[month_names.index(month)]
    return False

从日期获取月份和日期的名称。使用该名称在天数列表中查找天数计数。然后将天数转换为int,并与该列表中的值进行比较。如果day不能转换为int,只需返回False。

我建议为每个月的最大天数制作一个字典,例如:days_in_month={一月:31,…},并使用days_in_month.getclean_date[0]。所以你也不需要x或清洁日期[1]

我希望我的答案就是你想要的

首先,我有一个包含所有月份及其最大天数值的字典。我们使用字典month\u dictionary检查输入的day值与输入的月份名称,结果如下:if day>month\u dictionary.getmonth:。如果“天”值大于当月的“最大天”值或小于0,则函数将返回False并告诉您为什么要删除它(如果需要)

第二个函数只是有一个花哨的线条,使输入日期更容易。month,dayRAW=date.split在程序看到字符串中有空格时,将1个输入字符串拆分为2个变量month和dayRAW。在将日期字符串转换为数字day=intdayRAW后,它们将以与第一个函数相同的方式使用

mx0似乎也有我所写内容的浓缩版本。干得好,伙计


我也希望这能帮助其他路过的人:

这里已经回答了@pushpendrachauhan我的错,我编辑了我的帖子,以表明我们不允许使用做大部分工作的内置函数。您正在寻找.index方法:x=month\u names.indexclean\u date[0],然后是month中的days\u[x]应该给你当月的天数。此外,诸如a=a或b=b之类的语句也不起任何作用。if语句不必有else子句。如果不需要,您可以简单地省略else。与其使用变量a和b来保持您的状态,不如在看到错误时简单地返回False。然后,如果到达函数末尾时没有出现错误,则返回True。例如,在您的第一次测试中,如果clean_date[0]不在month_names:return false中,我了解所有内容,但maxsplit=1,您能解释一下它是如何工作的吗?如果您有闰年,而月份是二月,你还想把期限限制在28天吗?@MallikSai我想这已经超出了我的修订范围given@didleyr6s当maxspit=1时,您将始终得到两个元素:month和string的其余部分,因为它将在第一个空格后停止拆分。
def is_a_valid_date(date):
    month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
                   "November", "December"]
    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    month, day = date.split(maxsplit=1)

    if day.isdigit():
        return int(day) <= days_in_month[month_names.index(month)]
    return False
month_dictionary = {"January": 31,
                "February": 28,
                "March": 31,
                "April": 30,
                "May": 31,
                "June": 30,
                "July": 31,
                "August": 31,
                "September": 30,
                "October": 31,
                "November": 30,
                "December": 31}

def is_a_valid_date_1(month, day):#Input the month with quotes

    if day > month_dictionary.get(month):#Finds the corresponding value(number of days) to the month name
        print("Day number is too large")
        return False
    elif day <= 0:
        print("Day number can't be zero or smaller")
        return False
    else:
        print("This is a valid date")
        return True

def is_a_valid_date_2(date):
    month, dayRAW = date.split(" ")#Separates the date string when it sees a space
    day = int(dayRAW)#Turns the string into a workable integer

    if day > month_dictionary.get(month):#Finds the corresponding value(number of days) to the month name
        print("Day number is too large")
        return False
    elif day <= 0:
        print("Day number can't be zero or smaller")
        return False
    else:
        print("This is a valid date")
        return True