[英]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+1
和i
以相反的方式提出同样的问题。
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.