Apr-22-2023, 05:06 AM
I am brand new to coding in general, and have recently started taking up python courses. For one assignment, the task is to create a program which determines if a vanity plate is valid or not. The rules 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.”
Every other assignment so far has been relatively straight forward, but this one I just can't get right for some reason. I finally have it mostly complete but I keep getting the same two errors in the test system, which are:
:( input of CS50 yields output of Valid
expected "Valid", not "Invalid\n"
:( input of ECTO88 yields output of Valid
expected "Valid", not "Invalid\n"
I have NO IDEA why these two don't seem to work. They both print 'Invalid' when they should print 'Valid.' Not sure if my brain is just fried from trying over and over to work around it, but I would appreciate any help.
My code is below:
“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.”
Every other assignment so far has been relatively straight forward, but this one I just can't get right for some reason. I finally have it mostly complete but I keep getting the same two errors in the test system, which are:
:( input of CS50 yields output of Valid
expected "Valid", not "Invalid\n"
:( input of ECTO88 yields output of Valid
expected "Valid", not "Invalid\n"
I have NO IDEA why these two don't seem to work. They both print 'Invalid' when they should print 'Valid.' Not sure if my brain is just fried from trying over and over to work around it, but I would appreciate any help.
My code is below:
# Dictionary for punctuation punctuation = {"!", "#", "$", "%", "&", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "^", "]", "^", "_", "`", "{", ",", "|", "}", "~"} # Main Function def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") # "is_valid" Function def is_valid(p): if len(p) < 2: return False elif len(p) > 6: return False elif contains_punctuation(p) == True: return False elif does_not_begin_with_letters(p) == True: return False elif zero_check(p) == False: return False elif number_check(p) == False: return False else: return True # Checks if plate has any punctuation___COMPLETE def contains_punctuation(x): for char in x: if char in punctuation: return True # Checks if plate does not begin with 2 letters def does_not_begin_with_letters (a): if a[0].isalpha() == False or a[1].isalpha() == False: return True # Checks numbers: they must come at the end, not in the middle ## First number cannot be "0" ### AA1230 == Valid | AA12BB/AA12B3/AA0123 == Invalid #### Check "0" first def zero_check(z): i = 0 while i < len(z): if z[i].isalpha() == False: if z[i] == "0": return False else: break i += 1 ##### Check numbers second def number_check(n): j = 0 while j < len(n): if n[j].isnumeric() == True: if n[j + 1].isalpha() == False: return False else: break j += 1 # Run main function to complete main()Thank you for any help!