-2

Right now, i'm on a problem that prompts me to make a program based on license plates in Massachusetts, with a few conditions.

1.“All vanity plates must start with at least two letters.” 2.“… vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.” 3.“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. 4.The first number used cannot be a ‘0’.” 5.“No periods, spaces, or punctuation marks are allowed.”

Here is the code i have done so far, but it has a few problems. When i run it on check 50, i get some outputs but a majority of answers don't print anything...

:( input of CS05 yields output of Invalid Did not find "Invalid" in ""

Does anyone have any ideas why this is happening? I don't really know exactly what i did, but it may be the loops i added to fill each condition separately.

Code

def main():
    plate = input("Plate: ")
    if is_valid(plate):
    print("Valid")
    else:
        print("Invalid")


def is_valid(s):
    if len(s) < 2 or len(s) > 6:
    return False

    elif s[0].isalpha() == False or s[1] == False:
    return False
i = 2
while i < len(s):
    if s[i].isdigit() == True: 
        if s[i + 1].isalpha() == True: 
            return False
    else:
        break
i = i + 1 


j = 0
while j < len(s): 
    if s[j].isalpha() == False: 
        if s[j] == "0": 
            return False

    else:
        break
j = j + 1 
for char in s:
    if char in [" ", "?", "!", "."]:
        return False

return True 

main()

1
  • 4
    This can't be the code you ran, as this one contains syntax errors (see the missing indent after the first if, for example - and many more). You need to provide the exact code you ran, and clear examples of cases where it doesn't work as expected. Commented Nov 29, 2024 at 14:13

1 Answer 1

1

this will be my first stack overflow contribution, so i hope it helps. I pasted your code snippet in my editor and had some syntax errors. it was all due to indentation, which may just be a result of it being copy-pasted a couple of times. I also had to add your main() function call to the end to start the program. Once i did that, your code ran, but my output was nothing to the console as well.

I first added some print statements throughout your function to see if it was breaking at any particular point. No output still. Then I tried to print the value of the function itself with print(is_valid(plate)) - no output still. Then i assigned it to a variable and still got no output. it was clear the function was never terminating. In other words it never reached True or False. It had to be stuck in an infinite loop somewhere.

i = 2
while i < len(s):
    if s[i].isdigit() == True: 
        if s[i + 1].isalpha() == True: 
            return False
    else:
        break
i = i + 1

this 'i = i + 1' is outside of the while loop, meaning it will never execute until you exit the while loop. And the while loop will never terminate if i never reached len(s)

Fix that and your code will run, but you'll also get an index error for that loop. this is because index and length are different. You can have 6 letters(a length of 6). but since your index (plate[0]) starts at 0, your last letter will be index[5] 'i' will continue to increment to 6 though since its being compared to len(plate). Easy fix though - Just make sure you change it to:

len(plate)-1

I didn't fix your second while loop with the j variable, so you'll want to do that. its good debugging practice. Here's a snippet with your mostly working code. You can delete my print statements and fix the j while loop. I moved your main function to the bottom for my own readability, but that's just personal, nothing wrong with how you had it. Your control logic seems to work well, and a tried a few plates that seem to work, but you'll want to try a few more, because i think i still see some bugs.

    def is_valid(s):
    if len(s) < 2 or len(s) > 6:
        print('failure1')
        return False

    elif s[0].isalpha() == False or s[1] == False:   < call .isalpha() on the other side of this statement to actually check it, instead of checking the value of s[1] for False
        print('failure2')
        return False
    i = 2
    while i < len(s)-1:
        if s[i].isdigit() == True:
            if s[i + 1].isalpha() == True:
                print('failure3')
                return False
        else:
            break
        i = i + 1


    j = 0
    while j < len(s):
        if s[j].isalpha() == False:
            if s[j] == "0":
                print('failure4')
                return False

        else:
            break
    j = j + 1
    for char in s:
        if char in [" ", "?", "!", "."]:
            return False

    return True

def main():
    plate = input("Plate: ")
    print(plate)
    returned_value =is_valid(plate)
    print(returned_value)
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")
main()

One more thing I'd like to point out. The bug you had - an indentation error- is an easy mistake to make and a common symptom of having many nested for/while loops and if/else statements. Once you get your code working, look at refactoring some of it if possible to make it really stand out. If you do this, it will help with debugging in the future. Once again, hope that helps!

Sign up to request clarification or add additional context in comments.

3 Comments

you still have some indentation errors in your code (in the first line). Also you have some syntax error when you do < call .isalpha() on the.... if this is a comment it should be preceded by #
my code doesn't have indentation errors, but the indentations in the snippet got messed up when i pasted it. i found myself having to correct multiple snippets. and yeah i should've commented that note. is this a response to help with the question or just a critique of the advide?
This is a comment so you can edit your question and fix the problems mentioned, so the code in the question is a valid answer. As is now it presents problems that prevent the code from being executed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.