I just started learning python using the CS50 course today, and after I made it to week2 practice set I am stuck with this question "Vanity Plates". I have no background in coding before today and I am kinda overwhelmed by the sudden jump in difficulty :( The whole question is as follow:
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).
I wrote a code and when I ran it using the given tests, some of them passed and some failed. For the failed tests my code ran after input but did not output any result and I will have to terminate it. I am not sure what went wrong as there is no syntax error and I really can't spot the mistake. I will copy my code below and hopefully able to find some help. Much appreciated :)
Note : Since this question is posted here before I did look through previous responses and solutions, and while I understood some of them I will like to know what went wrong in my code to learn from mistakes.
def main():
plate = input("Plate: ").upper()
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
n = 0
flag = True
#check for length
if len(s) < 2 or len(s) > 6:
flag = False
#check for punctuations by eliminating upperchar/num unicode
for i in s:
if (ord(i) < 48) or (57 < ord(i) < 65) or (90 < ord(i)):
flag = False
#check for first two letter
for i in s[0:2]:
if ord(i) < ord("A"):
flag = False
#check if there are letters after numbers
for i in range(0, len(s)):
while (ord(s[i]) < ord("A")) and (i < (len(s)-1)):
n = n + 1
if ord(s[i+1]) > 57:
flag = False
#check if first number is 0
if (48 <= ord(s[i]) <= 57) and (n == 1):
if s[i] == 0:
flag = False
return flag
main()
Test failed : CS50 , ECTO88 , CS05 , 50 , CS50P2 , PI3.14
Test passed : NRVOUS , H , OUTATIME
Python has a number of functions which will simplify the checks to be performed.
To determine if a letter appears after a digit you need to find and compare the position of the last letter and the first digit in the string.
def is_valid(s):
# Check if the given string meets length requirements.
if len(s) < 2 or len(s) > 6:
return False
# Check if all characters are alphanumeric
if not s.isalnum():
return False
# Check if the string starts with at least two letters.
if not s[:2].isalpha():
return False
# Find the position of the first digit in the string
first_digit_index = 99 # A sensible default value
for index, char in enumerate(s):
if char.isdigit():
first_digit_index = index
break
# Find the position of the last letter in the string
last_alpha_index = -1
for index, char in enumerate(s[::-1]):
if char.isalpha():
last_alpha_index = len(s) - index
break
# Check if the first digit appears before the last letter
if first_digit_index < last_alpha_index:
return False
return True