Eugenia-Tate-Duke commited on
Commit
63c91ee
·
1 Parent(s): 0acc430

Add is_valid_vin helper function

Browse files
Files changed (1) hide show
  1. cli_demo.py +22 -9
cli_demo.py CHANGED
@@ -12,7 +12,20 @@ Administration) VIN decoding API via the helper module `nhtsa_api_call.py`.
12
  Usage:
13
  python cli_demo.py
14
  """
15
- import nhtsa_api_call
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def main():
18
  """
@@ -26,15 +39,15 @@ def main():
26
 
27
  while True:
28
  vin = input("Please enter a valid VIN or 0 (zero) to exit: \n").strip()
29
- # empty vin condition
30
- if not vin:
31
- print("⚠️ VIN cannot be empty. Please try again.\n")
32
- continue
33
- # exit condition
34
- elif vin == "0":
35
- print("👋 Exiting VIN lookup. Goodbye!")
36
  break
37
- # perform VIN lookup
 
 
 
 
38
  else:
39
  year, make, model = nhtsa_api_call.get_vehicle_info(vin)
40
  if year and make and model:
 
12
  Usage:
13
  python cli_demo.py
14
  """
15
+ import nhtsa_api_call, re
16
+
17
+ def is_valid_vin (vin):
18
+ """
19
+ Check whether vin is valid.
20
+ A valid VIN is a VIN of 17 alphanumeric characters long and can not
21
+ include I, O, Q.
22
+ """
23
+ vin = vin.upper()
24
+ if len(vin) != 17:
25
+ return False
26
+ if not re.match(r'^[A-HJ-NPR-Z0-9]{17}$', vin):
27
+ return False
28
+ return True
29
 
30
  def main():
31
  """
 
39
 
40
  while True:
41
  vin = input("Please enter a valid VIN or 0 (zero) to exit: \n").strip()
42
+ # exit condition gets checked first
43
+ if vin == "0":
44
+ print("👋 You chose to exit VIN lookup. Goodbye!")
 
 
 
 
45
  break
46
+ # check if vin is valid, and if not print error message
47
+ if not is_valid_vin(vin):
48
+ print("⚠️ Invalid VIN format. Must be 17 letters/numbers (no I, O, Q). Try again.\n")
49
+ continue
50
+ # vin is valid, perform VIN lookup
51
  else:
52
  year, make, model = nhtsa_api_call.get_vehicle_info(vin)
53
  if year and make and model: