Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from re import match

def validate_registration(plate):
  # regular expression to match valid registration
  # NB. only accepts new-style registrations, e.g. AB12 CDE
  return (match(r"[A-Z][A-Z]\d\d\s[A-Z][A-Z][A-Z]", plate) != None)

# return decimal version of time, e.g. 10:30 = 10.5
def decimalise(time):
  # is the time in the right format (i.e. does it have a :)?
  valid = time.count(":") == 1
  if valid:
    times = time.split(":")
    try:
      return float(times[0]) + float(times[1])/60
    except:
      print("This is not a valid time.")
      valid = False
  # return value to show that the time isn't valid
  return -1

def speed(start, end):
  # if end time is before start time...
  if start >= end:
    # ... assume that we've gone into the next day
    end += 24
  # speed = distance/time (and the distance is one mile)
  return round(1/(end-start),2)

print("Please use the 24-hour clock for all times.")
# ensure that end has a value
end = -1
# input times and convert to decimal value
start = decimalise(input("Time at first camera (HH:MM): "))
# only ask for the second time if the first one is valid
if start >= 0:
  end = decimalise(input("Time at second camera (HH:MM): "))
# only carry on if both times are valid
if start >= 0 and end >= 0:
  plate = input("Registration? ")
  print("Speed:", speed(start,end), "mph")
  print("Valid registration:",validate_registration(plate))