Python Forum
Beginner, problem with CS50 vanity plates problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner, problem with CS50 vanity plates problem
#1
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:
# 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!
Reply
#2
The issue you have is with your number_check() function.

On the first and second pass, the if n[j].isnumeric(): will return False, because n[0] and n[1] are both alpha in the string CS50, on the third pass, n[2] (which is the value of the j counter at this point) will index the '5', so the if n[j].isnumeric(): will return True, at which point the if n[j + 1].isalpha() == False: will return False, because j+1 = 3, which is the last index of the string, which is the '0'.

You can check that the the first the places are alpha, with if plate[:2].isalpha():, which will return True, given plate = 'CS50' You can also check that the last place is a number, with if plate[-1].isnumeric(): which will also return True for plate = 'CS50'

Try to write your code, so that the logic checks use the returns of the functions, as the default action, so that you don't need to have code such as if z[i].isalpha() == False:.

I hope you find these tips to be helpful and be able to see how, by using them, you can shorten you code and make it more human readable.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
(Apr-22-2023, 06:36 AM)rob101 Wrote: The issue you have is with your number_check() function.

On the first and second pass, the if n[j].isnumeric(): will return False, because n[0] and n[1] are both alpha in the string CS50, on the third pass, n[2] (which is the value of the j counter at this point) will index the '5', so the if n[j].isnumeric(): will return True, at which point the if n[j + 1].isalpha() == False: will return False, because j+1 = 3, which is the last index of the string, which is the '0'.

You can check that the the first the places are alpha, with if plate[:2].isalpha():, which will return True, given plate = 'CS50' You can also check that the last place is a number, with if plate[-1].isnumeric(): which will also return True for plate = 'CS50'

Try to write your code, so that the logic checks use the returns of the functions, as the default action, so that you don't need to have code such as if z[i].isalpha() == False:.

I hope you find these tips to be helpful and be able to see how, by using them, you can shorten you code and make it more human readable.
Oh wow, I could not see that for some reason. I was able to use what you said and change the code around to find a way for it to pass all of the tests, so thank you for the help!

Also, thank you for the tip. Like I said, I'm brand new to the coding and I see now how I overcomplicated some of this code by using logic like "str.isalpha() == False." I'll definitely make a note of that for future coding. Thank you again for the help and the tips, I appreciate it!
Reply
#4
(Apr-22-2023, 08:16 PM)Chief816 Wrote: Thank you again for the help and the tips, I appreciate it!

You are very welcome and happy coding.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tic Tac Toe problem JustDarkWTF 2 1,761 Jan-17-2022, 02:00 AM
Last Post: BashBedlam
  Problem 6.b nman52 4 2,440 Dec-26-2020, 03:34 PM
Last Post: DeaD_EyE
  Problem 5 nman52 6 2,488 Nov-29-2020, 04:25 AM
Last Post: nman52
  Try except problem MarcJuegos_YT 2 1,754 Aug-25-2020, 02:18 PM
Last Post: MarcJuegos_YT
  another problem :) raymond2688 28 9,826 Aug-22-2019, 10:02 AM
Last Post: anagarcia
  Help with beginner problem? glkolper 1 1,857 Sep-29-2018, 03:32 AM
Last Post: ichabod801
  I need Help in this problem Mostafa6687 2 3,103 Oct-12-2017, 04:51 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020