Question
Credit card numbers follow a standard system. For example, Visa,MasterCard, and Discover Card all have 16 digits, and the firstdigit serves to indicate the card
Credit card numbers follow a standard system. For example, Visa,MasterCard, and Discover Card all have 16 digits, and the firstdigit serves to indicate the card brand. All Visa cards start witha 4; MasterCard cards always starts with a 5; and Discover cardsstart with a 6.
The first function you will write should be called ‘cardType.Your function should take one input variable, astring representing a card number.
The function should return a single string. Thestring should have one of five values: ‘Visa’, MasterCard’,‘Discover Card’, ‘Unknown’, or ‘Invalid’. Make sure to watchcapitalization and spelling!
Your function should first make sure that the input card numberhas the correct number of digits (16). Remember, the input value isa string, not an int. Your function should return ‘Invalid’ if thecard number does not have the correct number of digits and yourfunction should exit. Next, your function should check the firstdigit of the card number and return the following based on thatfirst digit: return ‘Visa’ if the digit is a 4; return ‘MasterCard’if the digit is a 5; return ‘Discover’ if the digit is a 6; andreturn ‘Unknown’ if the card number starts with anything else.
T esting:
cardType(‘2845682912345’)
Should return:
Invalid cardType(‘4124836490234794’) Should return:
Visa
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started