Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cs50 import get_string
- # Function to check card number against Luhn's Algorithm
- def check(number):
- # Create checksum variables
- length = len(number)
- sum1 = 0 # Var to STORE each digit in luhnstep2 after multiplying every other number, beg with second to last, by 2
- sum2 = 0 # Var to SUM numbers stored in luhnstep2
- sum3 = 0 # Var to sum numbers stored in luhnstep3 (i.e., nums not multiplied by 2 in sum2/luhnstep2)
- luhnstep2 = [] # List in which sum2 helps store each digit of the product from multiplying every other number by 2
- luhnstep3 = [] # List in which sum3 stores numbers not multiplied by 2
- # Checksum operations for even-digit cards
- if length % 2 == 0:
- for i in range(length):
- # Check if i in iteration is an even num; if so, multiply by two and store product in sum1
- if i % 2 == 0:
- sum1 = int(number[i]) * 2
- # iterate over sum1 and store each indiv digit in list luhnstep2
- for i in range(len(str(sum1))):
- luhnstep2.append(str(sum1)[i])
- # If not an even digit in iteration, store digit in list which holds card numbers not used in "multiply by 2" portion of Luhn's algorithm
- else:
- luhnstep3.append(number[i])
- # Checksum operations for odd-digit cards
- else:
- for i in range(length):
- # Check if i in iteration is an odd num; if so, multiply by two and store product in sum1
- if i % 2 != 0:
- sum1 = int(number[i]) * 2
- # iterate over sum1 and store each indiv digit in list luhnstep2
- for i in range(len(str(sum1))):
- luhnstep2.append(str(sum1)[i])
- # If not an odd digit in iteration, store digit in list which holds card numbers not used in "multiply by 2" portion of Luhn's algorithm
- else:
- luhnstep3.append(number[i])
- # Checksum validation
- # Add numbers multiplied by 2
- for i in range(len(luhnstep2)):
- sum2 += int(luhnstep2[i])
- # Add numbers not multiplied by 2
- for i in range(len(luhnstep3)):
- sum3 += int(luhnstep3[i])
- # Add both sets of sums together
- checksum = str(sum2 + sum3)
- # If sum result does not end in "0" then function returns "false"
- if (checksum[len(checksum) - 1]) != "0":
- return False
- # List to hold valid prefixes for the three cards we'll test for
- validprefix = [
- ('34', 'AMEX'),
- ('37', 'AMEX'),
- ('51', 'MASTERCARD'),
- ('52', 'MASTERCARD'),
- ('53', 'MASTERCARD'),
- ('54', 'MASTERCARD'),
- ('55', 'MASTERCARD'),
- ('4', 'VISA')
- ]
- # Get user to input a card number and reprompt if not a full digit string
- while True:
- cardnumber = get_string("Number: ")
- if cardnumber.isdigit() == True:
- break
- # Create flag variable
- i = 0
- # Create variable to evaluate just the two prefix numbers of user input
- prefix = cardnumber[0] + cardnumber[1]
- # Lookup prefix numbers in validprefix List and return name of card if found
- for digits, name in validprefix:
- # If prefix in card number matches one in the List, add one to flag variable then run checksum fn (above)
- # If checksum doesn't comply, print invalid; if name not in list move to next test
- if str(prefix) in digits:
- i += 1
- if check(cardnumber) == False:
- print("INVALID")
- break
- print(name)
- break
- # If card starts with a 4 then add 1 to flag var then run checksum since this is our
- # Only non-two digit validation in validprefix List; If checksum doesn't comply, print invalid
- elif digits[0] == '4':
- i += 1
- if check(cardnumber) == False:
- print("INVALID")
- break
- print(name)
- break
- # If flag variable never tripped, card must be invalid; print invalid
- if i == 0:
- print("INVALID")
Advertisement
Add Comment
Please, Sign In to add comment