pythonfunctionvalidationpython-3.xluhn

Python Credit Card Validation


I'm a beginner Python learner and I'm currently working on Luhn Algorithm to check credit card validation. I wrote most of the code, but I'm stuck with 2 errors I get 1st one is num is referenced before assignment. 2nd one I'm getting is object of type '_io.TextIOWrapper' has no len(). Further help/ guidance will be greatly appreciated.

These are the steps for Luhn Algorithm (Mod10 Check)

  1. Double every second digit from right to left. If this “doubling” results in a two-digit number, add the two-digit number to get a single digit.
  2. Now add all single digit numbers from step 1.
  3. Add all digits in the odd places from right to left in the credit card number.
  4. Sum the results from steps 2 & 3.
  5. If the result from step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.

Here's what my output is supposed to be

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

And here is the code.

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()

Solution

  • You just forgot to initialize 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())