简体   繁体   English

如何检查字符串中的数字后面没有跟字母?

[英]How can I check that a digit within a string is not followed by a letter?

The CS50 Problen Set 2 - Plates assignment is to write a program that will validate vanity plate input. CS50 问题集 2 - 印版作业是编写一个程序来验证印版输入。 The requirements 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.” “……个性牌最多可包含 6 个字符(字母或数字),最少包含 2 个字符。” “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;例如,AAA222 是可以接受的……化妆板; AAA22A would not be acceptable. AAA22A 是不可接受的。 The first number used cannot be a '0'.”使用的第一个数字不能是‘0’。” “No periods, spaces, or punctuation marks are allowed.” “不允许使用句号、空格或标点符号。” My code works, except for the third requirement: "Numbers cannot be used in the middle of a plate..." My code:我的代码有效,除了第三个要求:“数字不能在盘子中间使用......”我的代码:

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

def is_valid(plate):
    # Check that there is no punctuation
    if not plate.isalnum():
        return False

    # Check that the length is from 2 to 6 characters
    if len(plate) < 2 or len(plate) > 6:
        return False

    # Check to see if the first two letters are alphanumeric
    if not plate[0:2].isalpha():
        return False

    # Check to see that numbers do not start with "0"
    for i in range(len(plate)):
        # If the current character is a number and the previous character is a letter,
        # check if the number is "0".
        if plate[i].isnumeric() and plate[i-1].isalpha() and plate[i] == "0":
            return False

    # Check to see that numbers are not in the middle - this is where the problem lies !
    plate = plate[::-1]
    for i in range(len(plate)):
        if plate[i].isnumeric() and plate[i-1].isalpha():
            return False

    # If all checks pass, return True
    return True
main()

The last section in the is_valid function is supposed to check that a number is never followed by a letter. is_valid 函数的最后一部分应该检查数字后面是否没有字母。 It does return those inputs as invalid, but also rejects inputs such as "CS50" which should be valid.它确实将这些输入返回为无效,但也会拒绝应该有效的输入,例如“CS50”。 In the version above, I have reversed the order of the variable (plate) in an attempt to see if a number follows a digit (which is not allowed in reverse order).在上面的版本中,我颠倒了变量(plate)的顺序,试图查看数字是否跟在数字后面(不允许颠倒顺序)。 I have tried all (I think) combinations of plate[i] and plate [i-1] etc. I have tried substituting isdigit() for isnumeric(), without success.我已经尝试了 plate[i] 和 plate [i-1] 等的所有(我认为)组合。我尝试用 isdigit() 代替 isnumeric(),但没有成功。 I know there must be other ways of solving the problem, but I would really like to understand why this code is not working when the code immediately above it does.我知道必须有其他方法来解决问题,但我真的很想了解为什么当上面的代码起作用时这段代码不起作用。 When running the code in pythontutor.com, the test seems to fail at the end of the range, where there are two letters, which makes no sense to me.在 pythontutor.com 中运行代码时,测试似乎在范围末尾失败,那里有两个字母,这对我来说毫无意义。

You have an error with your index.您的索引有误。

if plate[i].isnumeric() and plate[i-1].isalpha():

Because it is python, at the start of the loop, the i-1 wraps around to the end of the string.因为它是 python,所以在循环开始时, i-1环绕到字符串的末尾。 Using i+1 and i raise the same problem the other way around.使用i+1i以相反的方式提出同样的问题。

You need to change your range() to start and end so that warping does not occur.您需要将range()更改为开始和结束,以便不会发生翘曲。

The same problem is also present in your previous check.你之前的检查也存在同样的问题。 You did not notice because there is an additional condition and the edge cases are filtered by the third check.您没有注意到,因为还有一个附加条件,并且边缘情况被第三次检查过滤掉了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 Python 中列表的另一个字符串是否跟在一个字符串后面? - How can I check if a string is followed by another string of a list in Python? 如何检查字符串列表中的字符串中是否存在大写字母? - How do I check if there is a uppercase letter in a string within a list of strings? 如何使用python检查字符串中的字母是否大写? - How can I check if a letter in a string is capitalized using python? 如何检查字符串中的字符是否为字母? (Python) - How can I check if character in a string is a letter? (Python) 如何从特定字母/数字开始itertools组合? - How can i start itertools combinations from specific letter/digit? 检查字符串是否包含任意数量的数字后跟特定字母[Python] - Check if a string contains any amount of numbers followed by a specific letter [Python] 我想要一个正则表达式来检查字符串的一部分,该字符串以数字开头,后跟连字符 6- 后跟 9 个字母数字字符。 (例如:6-ab12y05g7) - I want a regex to check a part of a string that begins with a digit followed by hyphen 6- followed by 9 alphanumeric characters. (ex : 6-ab12y05g7) 我如何检查一个字符串中是否有多个字母并识别这些出现中的每一个? - How can i check if there is a letter multiple times in a string and identify each of these occurances? 如何检查 TextArea 字符串是否在标签内? - How can I check if a TextArea string is within a tag? 如何删除字符串中的字母 - How can I delete the letter in string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM