简体   繁体   English

如何进行验证循环以确保用户在 a.split 输入中输入正确的次数?

[英]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.附带说明一下,研究生成器表达式、列表推导和内置函数,例如allany以进一步简化您的代码。 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.

相关问题 如何在 for 循环中进行 n 个输入并在以后使用所有输入? - How do I make n-amount of inputs in a for-loop and use all the inputs later? 如何使程序在循环代码处跟踪用户输入? - How do I make the program track user inputs at the loop code? 你如何确保用户在创建 object class Python 时输入正确的类型(在我的例子中是列表) - How do you make sure that the user inputs the correct type (list in my case) when creating an object class Python 你如何让某物在 python 中循环特定次数? - how do you make something loop a specific amount of times in python? 如何输入数字并使该数字显示一个符号多少次? - How do I input a number and make that number display a symbol that amount of times? 如何在 python 中为用户输入创建一个循环? - How do I make a loop for user input in python? 用户输入字母时输入失败。 我想检查一下以确保他们输入了一个号码 - Input failing when user inputs a letter. I want to check to make sure they have put in a number Python:如何使python计算总和以确保输入正确? - Python: how to make python calculate a sum to make sure an input is correct? 如何在Python中遍历列表以确保没有重复? - How do I loop through a list in Python to make sure there are no repeats? 数据验证:在选择正确的输入之前,如何使程序发出错误? - Data Validation: How do I make my program issue an error until the correct input is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM