[英]How do I make a validation loop to make sure the user inputs the correct amount of times in a .split input?
This may be kind of stupid but I've made a really roundabout way of validating a credit card number.这可能有点愚蠢,但我已经做了一个非常迂回的验证信用卡号码的方法。 I'm still really new at python and coding in general and wanted to make it so that I can validate the amount of digits, whether or not the input is numbers, and to also make it so I can print the input split up like this: xxx-xxxx-xxxx我在 python 和一般编码方面仍然很新,并且想要制作它以便我可以验证数字的数量,无论输入是否是数字,并且还可以让我可以打印输入拆分像这样: xxx-xxxx-xxxx
So far I have this (please excuse how messy and probably unnecessary a lot of it is!)到目前为止我有这个(请原谅它是多么的混乱和可能是不必要的!)
CreditOne = 0
CreditTwo = 0
CreditThree = 0
while True:
CreditOne, CreditTwo, CreditThree = input("Enter the credit card number (separate with spaces): ").split()
CreditCardList = [CreditOne, CreditTwo, CreditThree]
CreditCardNumber = "-".join(CreditCardList)
if CreditOne.isdigit() and CreditTwo.isdigit() and CreditThree.isdigit() and len(CreditOne) == 4 and len(CreditTwo) == 4 and len(CreditThree) == 4:
break
elif CreditOne == 0 or CreditTwo == 0 or CreditThree == 0:
print("Please input a valid credit card number.")
continue
else:
print("Please input a valid credit card number.")
continue
print(CreditCardNumber)
It does the job for the most part except for the fact that if the user just inputs something like 4 4 or like a singular letter it will get a ValueError:它完成了大部分工作,除了如果用户只是输入类似 4 4 或类似单数字母的内容,它将得到一个 ValueError:
ValueError: not enough values to unpack (expected 3, got 1)
Basically what I've been trying to do is create a validation that allows the loop to continue after the error and return to the start of the loop.基本上我一直在尝试做的是创建一个验证,允许循环在错误之后继续并返回到循环的开头。 I've tried a try except loop and it didn't work, I wanted to get a second opinion on this and maybe some help from somebody who understands what I'm trying to achieve with my code.我尝试了除循环之外的尝试,但它没有用,我想对此获得第二意见,也许可以从了解我试图用我的代码实现什么的人那里得到一些帮助。
Another way you could do this is with exceptions - this will help you deal with other errors as well:您可以这样做的另一种方法是例外 - 这也将帮助您处理其他错误:
while True:
try:
CreditOne, CreditTwo, CreditThree = input("Enter the credit card number (separate with spaces): ").split()
break
except ValueError:
print('Oops! That's not a valid number')
Instead of unpacking first, then combining them into a list, do it the other way around:与其先解包,然后将它们组合成一个列表,不如反过来:
CreditCardList = input("Enter the credit card number (separate with spaces): ").split()
if len(CreditCardList) == 3:
CreditOne, CreditTwo, CreditThree = CreditCardList
# ... do other stuff
else:
print("You must enter exactly 3 numbers")
As a side note, study generator expressions, list comprehensions and built-ins such as all
and any
to further simplify your code.附带说明一下,研究生成器表达式、列表推导和内置函数,例如all
和any
以进一步简化您的代码。 For example, the following line:例如,以下行:
if CreditOne.isdigit() and CreditTwo.isdigit() and CreditThree.isdigit() and len(CreditOne) == 4 and len(CreditTwo) == 4 and len(CreditThree) == 4:
can be rewritten as可以改写为
if all(c.isdigit() and len(c) == 4 for c in CreditCardList):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.