为什么Python中有条件陈述的一部分返回,而其他人还可以?

发布于 2025-02-05 15:33:09 字数 1519 浏览 1 评论 0原文

我需要编写一个代码,以打印出有效或无效的车牌输入字母和数字。条件是:前两个字符必须是字母,字符必须在2到6之间,如果使用数字,则不应是第一个,也不应在字母前出现数字。

我将下面的代码放在Thonny上,无法理解为什么len(l)== 4 def worder_order()返回none 并且不执行代码的下一行,而其他行则正常。我的代码应返回CSAA50的“有效”,但它返回无效。为什么?

另外,是否有一种优雅的方法来编写def valif_order()

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

def is_valid(s):
    if s[0:2].isalpha() and 6 >= len(s) >= 2 and s.isalnum() and valid_order(s):
        return True
    else:
        return False


def valid_order(c):
    n = []
    l = list(c[2:len(c)])

    for i in c:
        if i.isdigit():
            n += i
    if n and n[0] == "0":
        return False

    if len(l) == 2:
        if l[0].isdigit() and l[1].isalpha():
            return False
    if len(l) == 3:
        if l[0].isdigit():
            if l[1].isalpha() or l[2].isalpha():
                return False
        else:
            if l[1].isdigit() and l[2].isalpha():
                return False

    if len(l) == 4:
        if l[0].isdigit():
            if l[1].isalpha() or l[2].isalpha() or l[3].isalpha():
                return False
        else:
            if l[1].isdigit():
                if l[2].isalpha() or l[3].isalpha():
                    return False
            else:
                if l[2].isdigit() and l[3].isalpha():
                    return False
    else:
        return True

main()

I need to write a code that prints out valid or invalid for an input of letters and numbers for a license plate. The conditions are: first two characters must be letters, characters have to be between 2 and 6, and if numbers are used, 0 should not be the first, nor should a number appear before a letter.

I put the code below on Thonny and cannot understand why len(l) == 4 part of the conditional statement for def valid_order() returns None and does not execute the next line of the code whereas others work fine. My code should return "Valid" for CSAA50, but it returns invalid. Why?

Also, is there an elegant way to write def valid_order()?

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

def is_valid(s):
    if s[0:2].isalpha() and 6 >= len(s) >= 2 and s.isalnum() and valid_order(s):
        return True
    else:
        return False


def valid_order(c):
    n = []
    l = list(c[2:len(c)])

    for i in c:
        if i.isdigit():
            n += i
    if n and n[0] == "0":
        return False

    if len(l) == 2:
        if l[0].isdigit() and l[1].isalpha():
            return False
    if len(l) == 3:
        if l[0].isdigit():
            if l[1].isalpha() or l[2].isalpha():
                return False
        else:
            if l[1].isdigit() and l[2].isalpha():
                return False

    if len(l) == 4:
        if l[0].isdigit():
            if l[1].isalpha() or l[2].isalpha() or l[3].isalpha():
                return False
        else:
            if l[1].isdigit():
                if l[2].isalpha() or l[3].isalpha():
                    return False
            else:
                if l[2].isdigit() and l[3].isalpha():
                    return False
    else:
        return True

main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文