title: Python Credit Card Validation
body: <p>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. </p>
  
  <p>These are the steps for Luhn Algorithm (Mod10 Check)</p>
  
  <ol>
  <li>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.</li>
  <li>Now add all single digit numbers from step 1.</li>
  <li>Add all digits in the odd places from right to left in the credit card number.</li>
  <li>Sum the results from steps 2 &amp; 3.</li>
  <li>If the result from step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.</li>
  </ol>
  
  <p>Here's what my output is supposed to be</p>
  
  <pre><code>Card Number         Valid / Invalid
  --------------------------------------
  3710293             Invalid
  5190990281925290    Invalid
  3716820019271998    Valid
  37168200192719989   Invalid
  8102966371298364    Invalid
  6823119834248189    Valid
  </code></pre>
  
  <p>And here is the code.</p>
  
  <pre><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 &gt; 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 &gt;= 13 and num &lt;= 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()
  </code></pre>