Python 信用卡验证

新手上路,请多包涵

我是 Python 初学者,目前正在使用 Luhn 算法来检查信用卡验证。我写了大部分代码,但我遇到了 2 个错误,第一个错误是在赋值之前引用了 num。我得到的第二个是“_io.TextIOWrapper”类型的对象没有 len()。进一步的帮助/指导将不胜感激。

这些是 Luhn 算法(Mod10 Check)的步骤

  1. 从右到左每第二个数字加倍。如果这种“加倍”的结果是两位数,则将两位数相加得到一位数。
  2. 现在将步骤 1 中的所有个位数相加。
  3. 将信用卡号中奇数位的所有数字从右到左相加。
  4. 将步骤 2 和 3 的结果相加。
  5. 如果步骤 4 的结果可以被 10 整除,则卡号有效;否则无效。

这是我的输出应该是什么

Card Number         Valid / Invalid
--------------------------------------
3710293             Invalid
5190990281925290    Invalid
3716820019271998    Valid
37168200192719989   Invalid
8102966371298364    Invalid
6823119834248189    Valid

这是代码。

 def checkSecondDigits(num):
    length = len(num)
    sum =  0
    for i in range(length-2,-1,-2):
      number = eval(num[i])
      number = number * 2
      if number > 9:
          strNumber = str(number)
          number = eval(strNumber[0]) + eval(strNumber[1])
          sum += number
      return sum

def odd_digits(num):
    length = len(num)
    sumOdd = 0
    for i in range(length-1,-1,-2):
        num += eval(num[i])
    return sumOdd

def c_length(num):
    length = len(num)
    if num >= 13 and num <= 16:
    if num [0] == "4" or num [0] == "5" or num [0] == "6" or (num [0] == "3" and num [1] == "7"):
        return True
    else:
        return False

def main():
    filename = input("What is the name of your input file? ")
    infile= open(filename,"r")
    cc = (infile.readline().strip())
    print(format("Card Number", "20s"), ("Valid / Invalid"))
    print("------------------------------------")
    while cc!= "EXIT":
        even = checkSecondDigits(num)
        odd = odd_digits(num)
        c_len = c_length(num)
        tot = even + odd

        if c_len == True and tot % 10 == 0:
            print(format(cc, "20s"), format("Valid", "20s"))
        else:
            print(format(cc, "20s"), format("Invalid", "20s"))
        num = (infile.readline().strip())

main()

原文由 Apurva Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

你只是忘了初始化 num

 def main():
    filename = input("What is the name of your input file? ")
    infile= open(filename,"r")
    # initialize num here
    num = cc = (infile.readline().strip())
    print(format("Card Number", "20s"), ("Valid / Invalid"))
    print("------------------------------------")
    while cc!= "EXIT":
        even = checkSecondDigits(num)
        odd = odd_digits(num)
        c_len = c_length(num)
        tot = even + odd

        if c_len == True and tot % 10 == 0:
            print(format(cc, "20s"), format("Valid", "20s"))
        else:
            print(format(cc, "20s"), format("Invalid", "20s"))
        num = cc = (infile.readline().strip())

原文由 2ps 发布,翻译遵循 CC BY-SA 3.0 许可协议

首先,也许你应该删除多余的字符:

 def format_card(card_num):
    """
    Formats card numbers to remove any spaces, unnecessary characters, etc
    Input: Card number, integer or string
    Output: Correctly formatted card number, string
    """
    import re
    card_num = str(card_num)
    # Regex to remove any nondigit characters
    return re.sub(r"\D", "", card_num)

使用 Luhn 算法检查信用卡是否有效后:

 def validate_card(formated_card_num):
    """
    Input: Card number, integer or string
    Output: Valid?, boolean
    """
    double = 0
    total = 0

    digits = str(card_num)

    for i in range(len(digits) - 1, -1, -1):
        for c in str((double + 1) * int(digits[i])):
            total += int(c)
        double = (double + 1) % 2

    return (total % 10) == 0

原文由 Vadim Kuzmyk 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题