-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplates.py
More file actions
47 lines (39 loc) · 1.88 KB
/
plates.py
File metadata and controls
47 lines (39 loc) · 1.88 KB
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
43
44
45
46
47
"""
In Massachusetts, home to Harvard University, it’s possible to request a vanity license plate for your car, with your choice of letters and numbers instead of random ones. Among the requirements, though, are:
“All vanity plates must start with at least two letters.”
“… vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.”
“Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable … vanity plate; AAA22A would not be acceptable. The first number used cannot be a ‘0’.”
“No periods, spaces, or punctuation marks are allowed.”
In plates.py, implement a program that prompts the user for a vanity plate and then output Valid if meets all of the requirements or Invalid if it does not. Assume that any letters in the user’s input will be uppercase. Structure your program per the below, wherein is_valid returns True if s meets all requirements and False if it does not. Assume that s will be a str. You’re welcome to implement additional functions for is_valid to call (e.g., one function per requirement).
"""
def is_valid(s):
# Assuming uppercase letters
s = s.upper()
# Two Letters
if not s[:2].isalpha():
return False
# Min and Max Chars
elif not 2 <= len(s) <= 6:
return False
# No periods, spaces, or punctuation marks are allowed
elif not s.isalnum():
return False
# The first number used cannot be a ‘0’
elif s[2] == "0":
return False
# Numbers Position
npos = False
for char in s[2:]:
if char.isnumeric():
npos = True
if char.isalpha() and npos:
return False
return True
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
if __name__ == "__main__":
main()