When I run my code through the compiler, it asks me for the credit card number. After I type in the number, there is a long pause after which the compiler returns 'Killed'. I looked this up online and it seems that my code might be using too much memory. If someone could point out to me where is it that I'm going wrong in my code, I would really appreciate it. On a side note, does my code come across as long-winded and can it afford to be more succinct? Thank you.

My code:

#Program to determine legitimacy of any credit card number
def main():

#Getting card number from user
cardNo = eval(input("What is the credit card number?\n"))
ccNo = cardNo
#Checking if card number is a valid type
#Check the first two digits OR first digit
firstTwoDigits = int(str(cardNo)[0:2])
firstDigit = int(str(cardNo)[0:1])
#Verify if American Express
if firstTwoDigits == 34 or 37:
    if len(str(cardNo)) is not 15:
        return None
#Verify if MasterCard
elif firstTwoDigits == 51 or 52 or 53 or 54 or 55:
    if len(str(cardNo)) is not 16:
        return None
#Verify if Visa
elif firstDigit == 4:
    if len(str(cardNo)) is not 13 or 16:
        return None

#Declaration of lists to be used later
multiplyList = []
noMultiplyList =[]
#Separation of digits into two different lists
#One of the lists to be x2, while the other left untouched
while cardNo>0:
    #Get last digit and preceding alternating digits of number
    noMultiply = cardNo%10
    #Storage of digits in list
    for i in range(10):
    #Get second-to-last digit and preceding alternating digits of number
    multiply = (cardNo/10)%10
    #Storage of digits in list
    for j in range(15):
        #Multiplying digits in list by two
        #Rectification if obtained number is double digit and not single digit
        if int(multiplyList[j]/10) > 0:
            #Splitting double digit number into two single digits
            digitBack = multiplyList[j]%10
            digitFront = (multiplyList[j]/10)%10
            #Replacing double digit no. with single digit
            multiplyList[j] = multiplyList.append(digitBack)
            #Appending the other single digit to end of list

#Total sum of digits in both lists
totalSum = sum(multiplyList) + sum(noMultiplyList)

#Final check to verify card number
if totalSum%10 == 0:
    if (ccNo > 339999999999999 and ccNo < 349999999999999) or (ccNo > 369999999999999 and ccNo < 379999999999999):
    elif (ccNo > 5099999999999999 and ccNo < 5599999999999999):
    elif (ccNo > 3999999999999 and ccNo < 4999999999999) or (ccNo > 3999999999999999 and ccNo < 4999999999999999):
    return 0
    return None

if __name__ == "__main__":

1 Answer 1


I don't see how while cardNo>0: could not cause an infinite loop. And don't use eval, it's evil.

  • Thank you for your help :)
    – Prav Elan
    Commented Feb 18, 2018 at 7:46

You must log in to answer this question.

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