问道题!谢谢大佬!
定义is_a_valid_date()函数,该函数将字符串作为参数传递给它。该函数返回一个布尔值,指示参数字符串是否为有效日期。该函数的前两行是:
month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
days_in_month =
其中month_names是有效月份名称的列表,days_in_month是包含每月最大天数的列表(请注意,2月设置为28天),例如month_names列表的第三个元素是3月,days_in_month列表的相应第三个元素表示3月有31天。
参数字符串可以包含任意数量的空格,但是月份名称必须始终从字符串开头的第一个非空格字符开始。
要测试的日期字符串的日期部分可能包含字母字符,从而使日期字符串无效。您会发现使用字符串方法isdigit()很有用,如果字符串由数字组成,则返回True,否则返回False。
Test:
print('1.',is_a_valid_date("January 21"))
print('2.',is_a_valid_date("Auust 3"))
print('3.',is_a_valid_date(" June 15B "))
print('4.',is_a_valid_date('February 0'))
print('5.',is_a_valid_date(" December 3K1"))
print('6.',is_a_valid_date("January 28 6"))
print('7.',is_a_valid_date(" January "))
print('8.',is_a_valid_date("May1"))
Result:
1. True
2. False
3. False
4. False
5. False
6. False
7. False
8. True
请问这个怎么写呢,我的代码系统测试出来有隐藏错误,请问怎么改呢
代码是:
def is_a_valid_date(word):
month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
days_in_month =
word = word.strip()
space = word.split(" ",1)
for i in range(len(space)):
space = space.strip()
if len(space) == 1:
return False
if space in month_names:
index1 = month_names.index(space)
if space.isdigit() and 0 < int(space) < days_in_month:
return True
return False
month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"]
month_names =
print(month_names)
days_in_month =
def is_a_valid_date(word):
word = word.strip()
space = word.split(" ",1)
print(space,len(space))
if len(space) == 1:
return False
for i in range(len(space)):
space = space.strip()
space = space.lower()
if space in month_names:
index1 = month_names.index(space)
print('index1',index1)
if space.isdigit() and 0 < int(space) < days_in_month:
return True
return False
print('1.',is_a_valid_date("January 21"))
print('2.',is_a_valid_date("Auust 3"))
print('3.',is_a_valid_date(" June 15B "))
print('4.',is_a_valid_date('February 0'))
print('5.',is_a_valid_date(" December 3K1"))
print('6.',is_a_valid_date("January 28 6"))
print('7.',is_a_valid_date(" January "))
print('8.',is_a_valid_date("May1")) 我不是大佬,找不出问题。
看了半天,也看不出什么!试试这样还有隐藏错误吗? xiangjianshinan 发表于 2020-5-21 06:52
我不是大佬,找不出问题。
看了半天,也看不出什么!试试这样还有隐藏错误吗?
好滴!我知道我的隐藏错误是什么了,就是月份开头在strip前面要先检查有没有空格,如果有就是Return False ,我没有注意到这个 本帖最后由 Twilight6 于 2020-5-21 08:27 编辑
if space.isdigit() and 0< int(space) <= days_in_month:
这样试试
def is_a_valid_date(word):
month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
days_in_month =
word = word.strip()
space = word.split(" ",1)
for i in range(len(space)):
space = space.strip()
if len(space) == 1 or len(space) > 2:
return False
if space in month_names:
index1 = month_names.index(space)
if space.isdigit() and 0< int(space) <= days_in_month:
return True
return False
print('1.',is_a_valid_date("January 21"))
print('2.',is_a_valid_date("Auust 3"))
print('3.',is_a_valid_date(" June 15B "))
print('4.',is_a_valid_date('February 0'))
print('5.',is_a_valid_date(" December 3K1"))
print('6.',is_a_valid_date("January 28 6"))
print('7.',is_a_valid_date(" January "))
print('8.',is_a_valid_date(" May 1 "))
print('9.',is_a_valid_date("February 28 "))
页:
[1]