Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:标准Uk reg的多字符串测试,为什么不工作?_Python - Fatal编程技术网

Python:标准Uk reg的多字符串测试,为什么不工作?

Python:标准Uk reg的多字符串测试,为什么不工作?,python,Python,以下是与主程序分离的部分,用于测试代码段是否有效。“yes”输出用于测试每个字符串方法,以查看错误发生的位置。这似乎不起作用,我不明白为什么,因为我已经把所有的测试标准英国注册,检查字符,因为他们似乎从一个输入 正如你所看到的,IM是一个非常低的级别(新手到Python)程序员,请考虑在你提供解决方案的时候。非常感谢 def main(): reg_number=input("What is your reg number?: ") correct_length = False

以下是与主程序分离的部分,用于测试代码段是否有效。“yes”输出用于测试每个字符串方法,以查看错误发生的位置。这似乎不起作用,我不明白为什么,因为我已经把所有的测试标准英国注册,检查字符,因为他们似乎从一个输入

正如你所看到的,IM是一个非常低的级别(新手到Python)程序员,请考虑在你提供解决方案的时候。非常感谢

def main():
    reg_number=input("What is your reg number?: ")
    correct_length = False
    first_two_char_capital = False
    next_two_char_digits= False
    next_char_space = False
    last_three_char_capital= False

    first_two_char_capital =reg_number[0:2]
    print(first_two_char_capital)
    next_two_char_digits=reg_number[2:4]
    print (next_two_char_digits)
    next_char_space=reg_number[4:5]
    print(next_char_space)
    last_three_char_capital=reg_number[5:8]
    print (last_three_char_capital)

    if len (reg_number) >= 8:
           correct_length = True
           print("yes1")

           for ch in reg_number:
               if first_two_char_capital.isupper():
                   first_two_char_capital = True
                   print("yes2")
               if next_two_char_digits.isdigit():
                   next_two_char_digits = True
                   print("yes3")
               if next_char_space.isspace():
                   next_char_space = True
                   print("yes4")
               if last_three_char_capital.isupper():
                   last_three_char_capital = True
                   print("yes5")
           if correct_length and first_two_char_capital and next_two_char_digits and next_char_space and last_three_char_capital:
               is_valid = False
               print(reg_number, "is a non-standard_registration")
           else:
               is_valid = True

               print(reg_number, "is a standard registration")
               return is_valid

main()

在这里只使用正则表达式要容易得多,例如:

import re

if re.match('[A-Z]{2}\d{2} [A-Z]{3}$', input('What is your regnumber:')):
    print('yeah it matches...')

在这里只使用正则表达式要容易得多,例如:

import re

if re.match('[A-Z]{2}\d{2} [A-Z]{3}$', input('What is your regnumber:')):
    print('yeah it matches...')

在这里只使用正则表达式要容易得多,例如:

import re

if re.match('[A-Z]{2}\d{2} [A-Z]{3}$', input('What is your regnumber:')):
    print('yeah it matches...')

在这里只使用正则表达式要容易得多,例如:

import re

if re.match('[A-Z]{2}\d{2} [A-Z]{3}$', input('What is your regnumber:')):
    print('yeah it matches...')

StackOverflow是一个为专业人士和热情的业余爱好者提供的论坛,而不是一个要求编写N个尽可能愚蠢的程序来匹配给定问题的论坛。我可以有另一个使用字符串切片的解决方案吗?StackOverflow是一个为专业人士和热情的业余爱好者提供的论坛,不是一个要求编写N个最愚蠢的程序来匹配给定问题的论坛。我可以有另一个使用字符串切片的解决方案吗?StackOverflow是专业人士和热情的业余爱好者的论坛,不是一个要求编写N个最愚蠢的程序来匹配给定问题的论坛。我可以有另一个使用字符串切片的解决方案吗,StackOverflow是一个为专业人士和热情的业余爱好者提供的论坛,而不是一个要求编写与给定问题相匹配的最愚蠢的程序的论坛。