【发布时间】: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