1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Vanity Plates - Python

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to solve the questions of CS50 Python course.

    Stuck at one of the questions: https://cs50.harvard.edu/python/2022/psets/2/plates/

    Solved everything, however the "no letter after numeral" part is very hard for me.

    I cannot understand why my solution does not work. Any idea?

    Please do not give different solution, I read several of them, I want to understand where is the error in my version.

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

    def is_valid(s):
    # check for non-letters and non-numbers
    if not s.isalnum():
    return False
    # check for correct length
    if len(s) < 2 or len(s) > 6:
    return False
    # check for correct first two characters
    if s[0].isdigit() or s[1].isdigit():
    return False
    # check for incorrect third character if there is any
    if len(s) > 2 and s[2] == "0":
    return False
    # check for errors in 4, 5, 6 length plate nemes:
    # 1. no first numeral with "0" value
    i = 0
    while i < len(s):
    if s.isdigit():
    if s == "0":
    return False
    else:
    break
    i += 1
    # 2. no letter after numeral
    for i in range(len(s)):
    if s.isdigit():
    if i < len(s)-1 and s[i+1:].isalpha():
    return False
    # all possible errors checked
    return True

    main()


    It seems the s[i+1:].isalpha() part never executes.

    Continue reading...

Compartilhe esta Página