【问题标题】:Python program giving unexpected results, why?Python程序给出了意想不到的结果,为什么?
【发布时间】:2016-05-06 22:44:15
【问题描述】:

我的python程序在正则表达式函数中给出了意想不到的结果,当我输入一个车牌进行识别时,它告诉我它是无效的,虽然它是有效的,但我不知道为什么?

如果您能告诉我出了什么问题并给出可能的解决方案,我将不胜感激,因为这是一项非常重要的家庭作业。

#Start
#04/02/2016
bad=[]#initialise bad list
data="Y"#initialise value to prevent infinite loop
standardNumberPlateObj=""
NumPlate=""
import re#import re functions
import pickle#import pickle function

print ("Welcome to the number plate recognition program.\n\n")
choice=input("Press 1 to input number plates and save\nPress 2 to read binary number plate file: ")
if choice == "1":
        while data=="Y":# while loop
            NumPlate = input("Input Registration number in format (XX01 XXX) *With a space at the end!*:\n\n") #user input for numberplate
            standardNumberPlateObj=re.match(r'\w\w\d\d\s\w\w\w\s', NumPlate, re.M|re.I)#validate that numberplate is valid

            if standardNumberPlateObj:
                print("Verification Success")
                data=input(str("Would you like to continue? (Y/N):"))

            else:
                print("Verification Failed")
                bad.append(NumPlate)#add numberplate to bad list
                data=input(str("Would you like to continue? (Y/N):"))#ask user to continue


        while data=="N":#saving number plates to file if user enters N
             f = open("reg.dat", "wb")
             pickle.dump(bad, f)
             f.close()
             print ("\nRestart the program to read binary file!")#ending message
             break

elif choice == "2":
             print ("\nBad number plates:\n\n")
             f=open("reg.dat", "rb")
             Registrations = pickle.load(f)
             print(Registrations)
             f.close()

else:
             print ("Please enter a valid choice!")

print ("\n END of program!")

【问题讨论】:

  • 你的输入是什么,确切地说,什么时候它是无效的?
  • 顺便说一句,你为什么要将字符串转换为字符串?例如,str("Would you like to continue? (Y/N):")
  • 请注意input 会解释输入!
  • 更多细节请masov?
  • 我指的是这个:stackoverflow.com/a/3800862/4271479 但是如果你使用的是 Python 3 那就没问题了。

标签: python


【解决方案1】:

如果没有示例输入以及预期结果和实际结果,真的无法判断。

但是从表达式\w\w\d\d\s\w\w\w\s 和提示中的示例 (XX01 XXX) 来看,我想说您的正则表达式最后需要一个空格,而您的输入没有提供一个空格。

【讨论】:

  • 我必须等待时间限制,否则我会的。 :(
猜你喜欢
  • 2019-09-16
  • 1970-01-01
  • 2019-05-19
  • 2017-01-05
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 2021-10-23
相关资源
最近更新 更多