1

I try to convert my Credit Solution from C to Python now.

But on Line 19 ( doubleCheck = ccNumber[i] * 2 ) i always get the error "TypeError: 'int' object is not subscriptable"!

How can i fix this?

from cs50 import get_int 
import sys

ccNumber = get_int("Please enter the cc number : ")

ccLength = len(str(ccNumber))

if ccLength != 13 and ccLength != 15 and ccLength != 16:
    sys.exit()

firstSum = 0
secondSum = 0
doubleCheck = 0

for i in range(ccLength, -1, -1):
    if i % 2 == 0:
        secondSum = secondSum + ccNumber[i]
    if i % 2 == 1:
        doubleCheck = ccNumber[i] * 2
        if doubleCheck >= 10:
            firstSum = firstSum + (doubleCheck % 10)
            firstSum = firstSum + (doubleCheck / 10)
        else:
            firstSum += doubleCheck;

totalChecksum = firstSum + secondSum

if totalChecksum % 10 == 0:
    if ccNumber[0] == 3 and ccLength == 15:
        if ccNumber[1] == 4 or ccNumber[1] == 7:
            print("AMEX", end="");
    elif ccNumber[0] == 4:
        if ccLength == 13 or ccLength == 16:
            print("VISA", end="")
    elif ccNumber[0] == 5 and ccLength == 16:
        if ccNumber[1] == 1 or ccNumber[1] == 2 or ccNumber[1] == 4 or ccNumber[1] == 5:
            print("MASTERCARD", end="")
else:
    print("INVALID", end="")
2

The function get_int returns an integer and therefore if you wish to access each character by its corresponding index consider first casting it to a string.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .