jerryjermaine

sentimental - credit.py

Mar 26th, 2022
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. from cs50 import get_string
  2.  
  3.  
  4. # Function to check card number against Luhn's Algorithm
  5. def check(number):
  6.     # Create checksum variables
  7.     length = len(number)
  8.     sum1 = 0 # Var to STORE each digit in luhnstep2 after multiplying every other number, beg with second to last, by 2
  9.     sum2 = 0 # Var to SUM numbers stored in luhnstep2
  10.     sum3 = 0 # Var to sum numbers stored in luhnstep3 (i.e., nums not multiplied by 2 in sum2/luhnstep2)
  11.     luhnstep2 = [] # List in which sum2 helps store each digit of the product from multiplying every other number by 2
  12.     luhnstep3 = [] # List in which sum3 stores numbers not multiplied by 2
  13.  
  14.     # Checksum operations for even-digit cards
  15.     if length % 2 == 0:
  16.         for i in range(length):
  17.             # Check if i in iteration is an even num; if so, multiply by two and store product in sum1
  18.             if i % 2 == 0:
  19.                 sum1 = int(number[i]) * 2
  20.                 # iterate over sum1 and store each indiv digit in list luhnstep2
  21.                 for i in range(len(str(sum1))):
  22.                     luhnstep2.append(str(sum1)[i])
  23.             # 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
  24.             else:
  25.                 luhnstep3.append(number[i])
  26.  
  27.     # Checksum operations for odd-digit cards
  28.     else:
  29.         for i in range(length):
  30.             # Check if i in iteration is an odd num; if so, multiply by two and store product in sum1
  31.             if i % 2 != 0:
  32.                 sum1 = int(number[i]) * 2
  33.                 # iterate over sum1 and store each indiv digit in list luhnstep2
  34.                 for i in range(len(str(sum1))):
  35.                     luhnstep2.append(str(sum1)[i])
  36.             # 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
  37.             else:
  38.                 luhnstep3.append(number[i])
  39.  
  40.     # Checksum validation
  41.     # Add numbers multiplied by 2
  42.     for i in range(len(luhnstep2)):
  43.         sum2 += int(luhnstep2[i])
  44.  
  45.     # Add numbers not multiplied by 2
  46.     for i in range(len(luhnstep3)):
  47.         sum3 += int(luhnstep3[i])
  48.  
  49.     # Add both sets of sums together
  50.     checksum = str(sum2 + sum3)
  51.  
  52.     # If sum result does not end in "0" then function returns "false"
  53.     if (checksum[len(checksum) - 1]) != "0":
  54.         return False
  55.  
  56.  
  57. # List to hold valid prefixes for the three cards we'll test for
  58. validprefix = [
  59.     ('34', 'AMEX'),
  60.     ('37', 'AMEX'),
  61.     ('51', 'MASTERCARD'),
  62.     ('52', 'MASTERCARD'),
  63.     ('53', 'MASTERCARD'),
  64.     ('54', 'MASTERCARD'),
  65.     ('55', 'MASTERCARD'),
  66.     ('4', 'VISA')
  67. ]
  68.  
  69. # Get user to input a card number and reprompt if not a full digit string
  70. while True:
  71.     cardnumber = get_string("Number: ")
  72.     if cardnumber.isdigit() == True:
  73.         break
  74.  
  75. # Create flag variable
  76. i = 0
  77.  
  78. # Create variable to evaluate just the two prefix numbers of user input
  79. prefix = cardnumber[0] + cardnumber[1]
  80.  
  81. # Lookup prefix numbers in validprefix List and return name of card if found
  82. for digits, name in validprefix:
  83.     # If prefix in card number matches one in the List, add one to flag variable then run checksum fn (above)
  84.     # If checksum doesn't comply, print invalid; if name not in list move to next test
  85.     if str(prefix) in digits:
  86.         i += 1
  87.         if check(cardnumber) == False:
  88.             print("INVALID")
  89.             break
  90.         print(name)
  91.         break
  92.  
  93.     # If card starts with a 4 then add 1 to flag var then run checksum since this is our
  94.     # Only non-two digit validation in validprefix List; If checksum doesn't comply, print invalid
  95.     elif digits[0] == '4':
  96.         i += 1
  97.         if check(cardnumber) == False:
  98.             print("INVALID")
  99.             break
  100.         print(name)
  101.         break
  102. # If flag variable never tripped, card must be invalid; print invalid
  103. if i == 0:
  104.     print("INVALID")
  105.  
Advertisement
Add Comment
Please, Sign In to add comment