代码之家  ›  专栏  ›  技术社区  ›  CS Geek

如何实现一个循环,其中:数字不能在输入的中间使用;他们最后一定要来?

  •  0
  • CS Geek  · 技术社区  · 2 年前
    def main():
        plate = input("Plate: ")
        if is_valid(plate):
            print("Valid")
        else:
            print("Invalid")
    
    
    def is_valid(s):
    
    # All vanity plates must start with at least two letters
        if s[0].isalpha() == False or s[1].isalpha() == False:
            return False
    
    # vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters
        if len(s) < 2 or len(s) > 6:
            return False
    
    # Numbers cannot be used in the middle of a plate; they must come at the end
    
    # TODO
    
    
    
    # No periods, spaces, or punctuation marks are allowed
        for c in s:
            if c in [".", " ", ",", "!", "?"]:
                return False
    
    # PASSES ALL TESTS
        return True
    
    
    main()
    

    这是我关于梳妆板的项目请求。。。输入有一定的标准,除一个标准外,所有标准都已成功实施。请帮我说一句话“ 数字不能用在盘子中间;他们最后一定会来 ".

    2 回复  |  直到 2 年前
        1
  •  0
  •   khelwood Muhammed Elsayed.radwan    2 年前

    以下是您可能的操作方法:

    def is_valid(s):
        if not (2 <= len(s) <= 6):
            return False
        if not s[:2].isalpha():
            # If the first two characters are not letters, it is invalid
            return False
        any_numbers = False
        for c in s:
            if c.isdigit():
                if c=='0' and not any_numbers:
                   # 0 cannot be the first digit
                   return False
                any_numbers = True
            elif c.isalpha():
                if any_numbers:
                    # If a letter comes after a number, it is invalid
                    return False
            else:
                # If the character is not a letter or number, it is invalid
                return False
        return True
    
        2
  •  0
  •   Kavi Harjani    2 年前

    首先检查第一个和第二个数字是否为数字,如果是,则为false 下一步检查长度是否最小为2,最大为6,否则为假 第三次检查是否从第一个数字开始,结束是数字,否则为假

    number = "A2AA222"
    
    
    def is_valid(number):
        if not 2 <= len(number) <= 6:
            return False
        elif number[0].isdigit() or number[1].isdigit():
            return False
        elif not number[:2].isalpha():
            return False    
    
        first_number = [num for num in number if num.isdigit()][0]
        first_number = number.index(first_number)
        number_list = number[first_number:]
    
        if number_list.isdigit():
            return True
        else:
            return False
    
    is_valid(number)