In: Computer Science
Credit Cards
Credit card numbers follow patterns.
Given that a number has satisfied the above criteria A and B the following steps are taken to validate the number:
Given the number:
4388576018402626
The steps 1 - 5 above would be (after determining that A an B above have been satisfied):
Step 1:
2 * 2 = 4
2 * 2 = 4
4 * 2 = 8
1 * 2 = 2
6 * 2 = 12 ( 1 + 2 = 3)
5 * 2 = 10 (1 + 0 = 1)
8 * 2 = 16 (1 + 6 = 7)
4 * 2 = 8
Step 2:
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
Step 3:
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Step 4:
37 + 38 = 75
Step 5:
75 % 10
You are to write a program that will prompt the user to enter a card number. You are then to determine if the number entered is valid or not. You are then to print to the screen if the number entered is a valid credit card number or not.
Your are to complete this program using functions. You are to have a function main in which you will only call the functions you have developed. The last line of your code is to be a call to main as below:
main()
Here are some valid numbers for testing purposes:
American Express |
378282246310005 |
American Express |
371449635398431 |
Discover |
6011111111111117 |
Discover |
6011000990139424 |
MasterCard |
5555555555554444 |
MasterCard |
5105105105105100 |
Visa |
4111111111111111 |
Visa |
4012888888881881 |
Visa |
4222222222222 |
Done in Python Format please
Solution ::
The solution for the above question is given below with the screenshot of output.
---------------------------------------------------------------------------------------------------------------
I have kept the logic simple and output as per the question.
I have used to numbers given in question:
If there is anything else do let me know in comments
---------------------------------------------------------------------------------------------------------------
--------------- CODE TO COPY -----------------------------------------------------------------------
def main(): # user will input the credit card number as a string # call the function isValid() and print whether the credit card number is # valid or not valid num = input("Enter how many number you want to check ?") for i in range( 0 , int(num) ): card_no = input("\nEnter a credit card number as a long integer: ") if ( isValid( card_no )): print( str(card_no) + " is valid.") else: print( str(card_no) + " is invalid.") def isValid(number) -> bool: # Return true if the card number is valid # hint you will have to call function sumOfDoubleEvenPlace() and sumOfOddPlace # It must have between 13 and 16 digits if ( len(number) < 13 or len(number) > 16 ): return False # It must startswith 4,5,6 0r 37 if ( ( number.startswith("4") == False) and ( number.startswith("5") == False) and ( number.startswith("6") == False) and ( number.startswith("37") == False) ): return False sum_of_even = sumOfDoubleEvenPlace(number) sum_of_odd = sumOfOddPlace(number) sum_even_odd = sum_of_even + sum_of_odd if ( sum_even_odd %10 == 0 ): return True return False def sumOfDoubleEvenPlace(number) -> int: # Get the result from Step 2 result = 0 for i in range( len(number) - 2, -1 , -2): num = int ( number[i] ) * 2 num = getDigit( num ) result += num return result def getDigit(number) -> int: # Return this number if it is a single digit, otherwise, return # the sum of the two digits if ( number > 9 ): number = int( str(number)[0] ) + int( str(number)[1] ) return number def sumOfOddPlace(number)-> int: # Return sum of odd place digits in number # Here are the two sample runs using the credit card numbers above: result = 0 for i in range( len(number) -1 , -1 , -2): num = int ( number[i] ) result += num return result print("Credit card number validation Program") print() main() ---------------------------------------------------------------------------------------------------------------
Output :
Please rate my answer. Thank you..............