Tuesday, 15 March 2011

python - How to import re and formatting code -


I'm trying to re-import and my original code has a format for a license plate, though I have a code But just do not know where to put it. I hope this is clear in my coding.

  import number_plate = re.match ('\ d {2} [az] {2} \ d {3}', '12MNB36'):  

I am trying to input the above code into my actual code shown below. Speed: speed = float () distance = 10 time = float (raw_input ('enter time')) speed = distance / time

  over_limit = [] file = open ("newfile. Number_plate = raw_input ('Enter the number plate') Print speed if> 31.2 9 28: M / s over_limit in # 70 mph. Change the append (number_platform) over_limit file. Written (number_papple) Other: Print ' OK 'file.close ()  

How can I write that try to do this Why doing:

  import again PL ATE_PATTERN = r '\ D {2} [AZ] {2} \ d {3}' with open ("newfile.txt", "w") such as f: over_limit = [] while true: distance = 10 print Time = raw_input ('enter pahr:') if time. Break (=) 'x': print ("bye") break speed = distance / float (time) print ("speed:% s"% speed) Speed ​​and 31.2 9 28: # 70 mph per hour if correct: number_plate = raw_input ('Enter the number plate' ') if re.match (PLATE_PATTERN, number_platform): over_limit.append (number_plate) f.write (Number_plate) Print ("Print plate (" Error: '% s' regex '% s' "% (number_plate, PLATE_PATTERN) ): In the action:  
 Enter the time: 1 Speed: 10.0 Speed ​​Enter the correct time: (speed 'OK') Other: Print ('Speed ​​OK')  

In action: .2 Speed: 50.0 Over Threshold! Enter the number plate: ABC 123 Error: 'ABC 123' rages '\ d {2} [A-Z] {2} \ d {3}' Number is not entered in the plate: 12 AB 345 plate was recorded. Enter time: x goodbye

There are a lot of changes in using the block with blocks for your file handle and output formatting, but most changes occur in the program's flow.

From the if / else statements, code deliberately uses the eternal loop while true which is "broken out" ( break ) When a certain position has been found. This is similar to do / while in other languages.

The idea is that the user enters at a time, which you only care about that you calculate it at that speed exceed the speed limit.

If it is not, then you will just ask another time.

It does this, only then Do you care about the license plate number? So then when you ask it you are compelling the user to enter a valid license plate - invalid people (which regex matches fail) show an error and the program user will try again Instructions to do

Once the user has written a matching plate number, it has been added to the over_limit list (which does not do anything in the way) and the file. Note that the file is being opened in a mode instead of 'w' mode, so every time you run the program, the output file will be "empty" .

When you are asked for some time, you can type 'x' and the program says "goodbye" and ends.

Note that since I started writing, it seems that you will need to update the PLATE_PATTERN expression.


No comments:

Post a Comment