Why am i getting TypeError: object of type 'int' has no len()

python

#1
## Credit Card Number Validation (Luhn Algorithm)

# Ask user for credit card number
ccnum = input ("Enter an 8-digit credit card number: ")

# Loop to make sure user inputs an 8 digit number only
while len(ccnum) != 8 or not ccnum.isdigit():
    ccnum = input("Incorrect!  Please enter an 8-digit credit card number:  ")

#Compute from the right, get the sum of every other odd space 
sumOdd = 0
for num in range(7, 0, -2):
    sumOdd = sumOdd + int(ccnum[num])
    print (sumOdd)

#also compute the sum of the digits from the right double the even indices
sumEven = 0
double = 0
for num in range(6, -1 -2):
    double = ccnum[num] * 2
    sumEven = sumEven + double[0] + double[1]
    print (sumEven)

if len(double) == 2: # if the number is two digits add together (eg. 18 is 1+8)
    sumEven = sumEven + double[0] + double[1]

else:
    sumEven = sumEven + double[0] # if not 2 digits then sum

#2

What version of python are you using? That matters

len() is a function to get length of list or string, it won’t work for integer

in python2, using input() will cause the input to be evaluated as code. Which is a security risk. use raw_input, then you get a string, on which you can use length, and then convert to integer


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.