Advertisement
UME14

cs50/sentimental/credit.py

Feb 23rd, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. # American Express
  2. # American Express uses 15-digit numbers
  3. # starts with 34 or 37
  4.  
  5. # MasterCard
  6. # MasterCard uses 16-digit numbers
  7. # starts with 51, 52, 53, 54, 55
  8.  
  9. # Visa
  10. # Visa uses 13- and 16-digit numbers
  11. # starts with 4
  12. # =======================================
  13.  
  14. # DEBUGGING NEEDED: problem with input
  15.  
  16. # credit card number
  17. credit = 0
  18.  
  19. # input
  20. inpt = input("Number: ")
  21.  
  22.  
  23. def inptFunc(x):
  24. '''just a simple function for input automation'''
  25. try:
  26. int(x)
  27. return int(x)
  28. except ValueError:
  29. y = input("Number: ")
  30. inptFunc(y)
  31.  
  32.  
  33. credit = inptFunc(inpt)
  34.  
  35. # calculate
  36. while True:
  37. credit_str = str(credit)
  38. count = 0
  39. temp = 0
  40.  
  41. # AmEx
  42. if len(credit_str) == 15 and credit_str[0] == '3':
  43. if credit_str[1] == '4' or '7':
  44.  
  45. # 2nd to last digits
  46. length = len(credit_str) - 2
  47.  
  48. # (1st step) make str
  49. temp_str = ""
  50.  
  51. for i in credit_str:
  52. if length >= 0:
  53. temp = int(credit_str[length]) * 2
  54. temp_str += str(temp)
  55.  
  56. # increment index
  57. length -= 2
  58.  
  59. # (2nd step) add all of 'em
  60. for i in range(len(str(temp_str))):
  61. count += int(temp_str[i])
  62.  
  63. # other digits
  64. length = len(credit_str) - 1
  65.  
  66. for i in credit_str:
  67. if length >= 0:
  68. count += int(credit_str[length])
  69. length -= 2
  70.  
  71. # check and out
  72. if count % 10 == 0:
  73. print("AMEX")
  74. break
  75. else:
  76. print("INVALID")
  77. break
  78.  
  79. # MasterCard
  80. elif len(credit_str) == 16 and credit_str[0] == '5':
  81. if credit_str[1] == '1' or '2' or '3' or '4' or '5':
  82.  
  83. # 2nd to last digits
  84. length = len(credit_str) - 2
  85.  
  86. # (1st step) make str
  87. temp_str = ""
  88.  
  89. for i in credit_str:
  90. if length >= 0:
  91. temp = int(credit_str[length]) * 2
  92. temp_str += str(temp)
  93.  
  94. # increment index
  95. length -= 2
  96.  
  97. # (2nd step) add all of 'em
  98. for i in range(len(str(temp_str))):
  99. count += int(temp_str[i])
  100.  
  101. # other digits
  102. length = len(credit_str) - 1
  103.  
  104. for i in credit_str:
  105. if length >= 0:
  106. count += int(credit_str[length])
  107. length -= 2
  108.  
  109. # check and out
  110. if count % 10 == 0:
  111. print("MASTERCARD")
  112. break
  113. else:
  114. print("INVALID")
  115. break
  116.  
  117.  
  118. # COPY-PASTE ramining: in Visa
  119. # Visa
  120. elif len(credit_str) == 13 or 16 and credit_str[0] == '4':
  121.  
  122. # 2nd to last digits
  123. length = len(credit_str) - 2
  124.  
  125. # (1st step) make str
  126. temp_str = ""
  127.  
  128. for i in credit_str:
  129. if length >= 0:
  130. temp = int(credit_str[length]) * 2
  131. temp_str += str(temp)
  132.  
  133. # increment index
  134. length -= 2
  135.  
  136. # (2nd step) add all of 'em
  137. for i in range(len(str(temp_str))):
  138. count += int(temp_str[i])
  139.  
  140. # other digits
  141. length = len(credit_str) - 1
  142.  
  143. for i in credit_str:
  144. if length >= 0:
  145. count += int(credit_str[length])
  146. length -= 2
  147.  
  148. # check and out
  149. if count % 10 == 0:
  150. print("VISA")
  151. break
  152. else:
  153. print("INVALID")
  154. break
  155.  
  156. # if no cards matched
  157. else:
  158. print("INVALID")
  159. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement