Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import sys
  2.  
  3. # check for valid input
  4. if len(sys.argv) == 2:
  5. try:
  6. int(sys.argv[1])
  7. if(len(str(sys.argv[1])) == 16):
  8. pass
  9. else:
  10. print("Not 16 digits!")
  11. sys.exit()
  12. except ValueError:
  13. print('Not an integer!')
  14. sys.exit()
  15. else:
  16. print('Not enough or too many command line arguments! n Proper use "python Check.py <credit card number here> " ')
  17. sys.exit()
  18.  
  19. def main():
  20. # put the digits into a list
  21. number = convertToList(sys.argv[1])
  22. sum = cardCheck(number)
  23. if (sum%10 == 0):
  24. print('Valid Card!')
  25. else:
  26. print('Invalid Card!')
  27.  
  28. #converts initial passed int variable to list
  29. def convertToList(num):
  30. numStr = str(num)
  31. numList = []
  32. for digit in numStr:
  33. numList.append(int(digit))
  34. return (numList)
  35.  
  36. def cardCheck(digitList, count = 0):
  37. sum = 0
  38. #if digit is every second digit multiply by 2
  39. if(count%2 == 0 & count < 15):
  40. digitList[count] = (digitList[count] * 2)
  41. #if is 2 digit number after multiplication
  42. if(digitList[count] >= 10):
  43. digitList[count] = addDigits(digitList[count])
  44. cardCheck(digitList, count + 1)
  45. else:
  46. cardCheck(digitList, count + 1)
  47. #progresses program
  48. elif(count < 15):
  49. cardCheck(digitList, count + 1)
  50. else:
  51. return 0
  52. for digits in digitList:
  53. sum += int(digits)
  54. return sum
  55.  
  56. #resolve 2 digit number conflict by adding the digits of the number and returning it
  57. def addDigits(num):
  58. list = str(num)
  59. sum = 0
  60. for digits in list:
  61. sum += int(digits)
  62. return sum
  63.  
  64. if __name__ == '__main__':
  65. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement