2016-05-06 12 views
0

Pythonプログラムが正規表現関数内で予期せぬ結果を出していますが、認識のためにナンバープレートを入力すると無効となりますが、有効ではありません。予期せぬ結果をもたらす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!") 
+1

あなたの入力は、正確には何が無効なのですか? –

+0

ところで、なぜ文字列を文字列に変換していますか?例えば、 'str("続行しますか?(Y/N): ")' –

+0

'input'が入力を解釈することに注意してください! – mastov

答えて

0

入力の例と予想された結果と実際の結果がないと実際には分かりません。

しかし、式\w\w\d\d\s\w\w\w\sとプロンプト(XX01 XXX)の例から判断すると、正規表現では最後にスペースが必要ですが、入力には空白がないとします。

+0

それは、ありがとうございます。 – Kappa145

+0

@ Kappa145:答えを受け入れてください。 – mastov

+0

私は期限を待たなければなりません。 :( – Kappa145

関連する問題