Posted on:

21 Jun 2023

0

Luhn's algorithm Alternate solution


credit_card = '228347687623'

#create a list of int
credit_card = [int(x) for x in credit_card]

#reverse the list
credit_card = credit_card[::-1]

def evaluate_creditCard(credid_card):
    double = 0
    single = 0
    for i,val in enumerate(credit_card):
        
        if i %2 != 0:
            #multiply digit with 2 and add the sum
            #need to convert int to str inorder to loop through the digits
            a = sum(int(x) for x in str(val*2))    
            
            double = double + a           
        else:
            single = single + val

    total = double + single
    
    if total%10 ==0:
        print('Credit card is valid')
    else:
        print('Credit card is not valid')
    
    
evaluate_creditCard(credit_card)

0 answers ( 0 marked as helpful)

Submit an answer