Skip to content

Instantly share code, notes, and snippets.

@MoorthySuresh
Created January 21, 2021 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoorthySuresh/d49282a3ee3d630178eaff529b8db0e1 to your computer and use it in GitHub Desktop.
Save MoorthySuresh/d49282a3ee3d630178eaff529b8db0e1 to your computer and use it in GitHub Desktop.
Cross checking the check-digit of a valid credit card
#Luhn algorithm
#Credit_Card=input("Enter your credit card number: ")
Credit_Card="379762088600299"
rCredit_Card=Credit_Card[::-1]#reversing the string
print(rCredit_Card)
sCredit_Card=rCredit_Card.strip()#removing spaces before and after a string, if any
r1Credit_Card=sCredit_Card[1:]
print(r1Credit_Card)#CheckDigit removed
total=0
for index, value in enumerate (r1Credit_Card):
if index%2==0: #extracting even position of digits from left to right.
x=int(value)*2 #doubling the even position of digits from left to right
if x>9: # add the digits of the resulting value, if the valus is more than 9
x=x-9
total=total+x
#print(total)
else:
x=int(value) #extracting odd position of digits from left to right.
total=total+x
total=total*9
check_digit=str(total%10)
print("Total",total)
#print("check",check_digit)
rCredit_Card=check_digit+r1Credit_Card[:]
Credit_Card=rCredit_Card[::-1]
print("YOUR CREDIT CARD NUMBER IS:", Credit_Card, "& THE CHECKDIGIT IS: ", check_digit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment