Validate Credit Card Number Using Luhn Algorithm Python
<p>I am trying to implement Luhn algorithm in Python. Here is my code</p>

<pre><code>def validate(n):
    if len(str(n)) &gt; 16:
        return False
    else:
        if len(str(n)) % 2 == 0:
            for i in str(n[0::2]):
                digit = int(str(n[i])) * 2
                while digit &gt; 9:
                    digit = sum(map(int, str(digit)))
            dig_sum = sum(map(int, str(n)))
            return True if dig_sum % 10 == 0 else False
        elif len(str(n)) % 2 != 0:
            for i in str(n[1::2]):
                digit = int(str(n[i])) * 2
                while digit &gt; 9:
                    digit = sum(map(int, str(digit)))
            dig_sum = sum(map(int, str(n)))
            return True if dig_sum % 10 == 0 else False
</code></pre>

<p>I keep getting the error</p>

<pre><code>TypeError: 'int' object has no attribute '__getitem__
</code></pre>