bocajbee

credit.py

Jun 1st, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. from cs50 import get_string
  2.  
  3. def main():
  4.  
  5.     CCnumber = get_string("Please enter a CC number: ") # get the credit card number from the user. this makes sure it's an integer, and that it's not empty automatically.
  6.     # print(CCnumber[0])
  7.     CCNumberLengh = len(str(CCnumber)) # get the lengh of the credit card number the user inputted
  8.  
  9.     if CCNumberLengh < 15 or CCNumberLengh > 16:
  10.         print("INVALID")
  11.         return 0
  12.  
  13.     # Check starting numbers of the credit card & return VISA, AMEX or MASTERCARD
  14.     elif CCNumberLengh == 15 and CCnumber[0] == "3" and (CCnumber[1] == "4" or CCnumber[1] == "7"): # Verify CC is American Express. All AA cards start with 34 or 37
  15.         if lunhsAlg(CCnumber) == 0:
  16.             print("AMEX");
  17.         else:
  18.             print("INVALID")
  19.  
  20.      #Verify CC is Mastercard. All MC's start with a 51, 52, 53, 54 or 55
  21.     elif CCNumberLengh == 16 and CCnumber[0] == "5" or CCnumber[0] == "2" and CCnumber[1] >= "1" and CCnumber[1] <= "5":
  22.         print("POO")
  23.         if lunhsAlg(CCnumber) == 0:
  24.             print("MASTERCARD")
  25.         else:
  26.             print("INVALID")
  27.  
  28.     # Verify CC is visa. All Visa's are 13 or 16 digits long and start with the string value of '4'
  29.     elif CCNumberLengh == 13 or CCNumberLengh == 16 and CCnumber[0] == "4":
  30.         print("WEE")
  31.         if lunhsAlg(CCnumber) == 0:
  32.             print("VISA")
  33.         else:
  34.             print("INVALID")
  35.     else:
  36.         print("INVALID")
  37.  
  38. def lunhsAlg(CCnumber):
  39.  
  40.     l = len(CCnumber)
  41.     s = 0
  42.     t = 0
  43.  
  44.     for i in range(l - 2, -1, -2):  # start at the end of the CC - 2 spaces against its len() then deincreiment to 0 by 2
  45.         t = int(CCnumber[i])
  46.         s = s + add_digits(t * 2)
  47.         #  print("This is s", s)
  48.  
  49.     print(i)
  50.  
  51.     for i in range(l - 1, -1, -2):  # start at the end of the CC - 1 space against its len() then deincreiment to 0 by 2
  52.         t = int(CCnumber[i])
  53.         s = s + t
  54.         # print("This is s after adding it to the other cc numbers t", s)
  55.  
  56.     if s % 10 == 0:
  57.         return 0
  58.  
  59.  
  60. def add_digits(n): # for this example we will pass in 17
  61.  
  62.     d = 0
  63.  
  64.     while n > 0:
  65.         d = d + (n % 10)
  66.         n = int(n / 10)
  67.  
  68.     print(d)
  69.     return d # return this integer value
  70.  
  71. if __name__ == "__main__":
  72.     main()
Add Comment
Please, Sign In to add comment