【发布时间】:2022-06-13 21:02:59
【问题描述】:
我正在用 python 编写一个程序,它需要一些字符串并通过一些条件对其进行测试
- 字符串的前 2 个字符必须是字母
2 。转向必须最多 6 个字符且至少 2 个字符
3 .字符串中间不能使用数字;他们必须在最后。例如,AAA222 是可以接受的……; AAA22A 是不能接受的
4 。使用的第一个数字不能是‘0’。”
5 . [' ', ',', ';', '-', '_'] 不允许使用这些字符
这是我目前的代码
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
total = is_N_1(s) + is_N_2(s) + is_N_3(s) + is_N_4(s) + is_N_5(s)
if total == 5:
#print (total)
return True
else:
#print(total)
return False
def is_N_1(s):
if len(s)<7 and len(s)>3:
return 1
else:
return 0
def is_N_2(s):
if s[0:2].isalpha():
return 1
else:
return 0
def is_N_3(s):
for i in s:
if s[-1].isalpha() and i.isnumeric():
return 0
else:
return 1
def is_N_4(s):
t = []
for i in s:
if i.isdigit():
t.append(i)
if len(t)<=0:
return 1
else:
if t[0] == '0':
return 0
else:
return 1
def is_N_5(s):
not_allow =[' ', ',', ';', '-', '_']
for i in s :
for _ in not_allow :
if i == _:
return 1
else :
return 0
main()
这是在输出中给出错误的输入
1 . input of "CS50"
expected "Valid", not "Invalid\n"
2 . input of "ECTO88"
expected "Valid", not "Invalid\n"
3 . input of "NRVOUS"
expected "Valid", not "Invalid\n"
任何人都可以看看这个并告诉我我做错了什么,我堆叠了2个小时,我没有找到解决方案?
【问题讨论】:
-
向我们展示您为程序提供的输入,以及它产生的输出。
-
您能否提供一个最小的测试用例,在该测试用例中您的脚本会产生错误的输出?
-
由 Desty 和 @Piotr-Grzybowski 修复,非常感谢你们俩
标签: python