Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

AmericanExpress;15;34,37ChinaT-Union;19;31ChinaUnionPay;16,19;62,88Dankort;16;5019DinersClub-CarteBlanche;14;300,301,302,303,304,305DinersClub-International;14;36,38,39DinersClub-USA&Canada;16;54Discover;16,19;6011,622126-622925,644,645,646,647,648,649,65InstaPayment;16;637,638,639JapanCreditBureau(JCB);16;19,3528-3589Maestro;16,19;5018,5020,5038,5893,6304,6759,6761,6762,6763MasterCard;16;51,52,53,54,55,222100-272099RuPay;16;60,6521,6522UATP;15;1Verve;16;506099-506198,650002-650027Visa;13,16,19;4VisaElectron;16;4026,417500,4405,4508,4844,4913,4917 I am trying to read from this file and then check to see when the user enters a credit card number, it would see

AmericanExpress;15;34,37ChinaT-Union;19;31ChinaUnionPay;16,19;62,88Dankort;16;5019DinersClub-CarteBlanche;14;300,301,302,303,304,305DinersClub-International;14;36,38,39DinersClub-USA&Canada;16;54Discover;16,19;6011,622126-622925,644,645,646,647,648,649,65InstaPayment;16;637,638,639JapanCreditBureau(JCB);16;19,3528-3589Maestro;16,19;5018,5020,5038,5893,6304,6759,6761,6762,6763MasterCard;16;51,52,53,54,55,222100-272099RuPay;16;60,6521,6522UATP;15;1Verve;16;506099-506198,650002-650027Visa;13,16,19;4VisaElectron;16;4026,417500,4405,4508,4844,4913,4917

I am trying to read from this file and then check to see when the user enters a credit card number, it would see if it would match a couple of numbers from the third cell and the length from the second cell, in order to get the type in the first cell. It's for a python homework that I am working on. These are the instructions of what I would need to do:

v> We will use the following file to identify credit cards: credit_card_types.ssv (ssv =

semi-colon separated values). This is a plain text file, where the three columns

correspond to the issuing network, length, and issuer identification number (prefix) of the

card.

These are just a few selected example rows from the file:

1: Issuing Network,Length,Issuer Identification Number (Prefix

2: American Express;15;34,37

3: Diners Club - International;14;36,38,39

4: China UnionPay;16,19;62,88

5: MasterCard;16;51,52,53,54,55,222100-272099

Row 1 is the header (not present in the file for convenience of fileIO). A cell is surrounded

by semicolons. A cell may have several numbers separated by commas, or separated by

a hyphen, in which case they represent a range. For example:

?

On row #2, AmEx cards have 15 digits, and they can start with 34 or 37.

?

Row 4 specifies that China UnionPay cards can have 16 or 19 digits and start with

62 or 88.

?

On row 5, we can see that MasterCard has 16-digit card numbers and they can

start with 51, 52, 53, 54, 55, or any sequence between 222100 and 272099,

inclusive.

You will have to read such a file and store the information in an appropriate data structure

to use in your code.

I already have the structure of the code to be split using csv and have ; as a delimiter. I used a for loop to read each line in the file. I also used the split since the second cell (the card length) has more than one length by a comma. Same thing for the third cell (issuer identification). I already created a method to check if the card sequence is valid and the luhn verified. It will be below. The sample output is supposed to be like this:

Please enter a credit card number:5558397375275489Credit card number: 5558397375275489Credit card type: MasterCardLuhn verification: Authentic.

image text in transcribedimage text in transcribed
def luhn_verified(credit_card_number) : Checks to verify whether the given credit card number is "Authentic' or "Fake" Args: credit_card_number - a credit card number to be verified Return: a string representation of whether the credit card number is "Authentic' or 'Fake" if ' -' in credit_card_number: credit_card_number = credit_card_number. replace(' -", "") if " 'in credit_card_number: credit_card_number = credit_card_number. replace(" ", ".) the_sum = 0 last_digit = int(str(credit_card_number ) [-1]) card_number_reverse = list(int(digit) for digit in str(credit_card_number) [-2::-1]) even_card_number_position = card_number_reverse[-1::-2] odd_card_number_position = card_number_reverse[ -2::-2] for odd_num in odd_card_number_position: odd_card_number_position = odd_num * 2 the_sum += odd_card_number_position if the_sum > 9: the_sum = the_sum - 9 for even_num in even_card_number_position: the_sum = the_sum + even_num if ((the_sum + last_digit) % 10) = = 0: return "Authentic' return "Fake" def is_valid (sequence) : Checks if the specified sequence is a valid credit card number. Args: sequence - the specified sequence Return: true if sequence is a valid credit card number; false otherwise required_length = re. compile(r"\\b\\d{13, 19)\\b") sixteen_digit = re. compile(r'((\\d(4))-(\\d(4))-(\\d(4))-(\\d(4))1(\\d(4)) (\\d(4)) (\\d(4)) (\\d{4)))") if required_length. match (sequence) or sixteen_digit. match(sequence): return True return Falsedef main(filename ) : Starts the script. try: with open (filename) as input_file: content = csv. reader (input_file, delimiter=";") credit_card_number = input("Please enter a credit card number: ") print( 'Credit card number: ' + credit_card_number) print( ' Credit card type: ' ) for line in content: fields = line issuing_network = fields[0] card_length = fields[1]. split(",") issuer_identification = fields[2]. split(",") if utils. is_valid(credit_card_number ) : second_answer = utils. luhn_verified (credit_card_number) print ( second_answer ) input_file . close() except IOError as error: print ( 'Problem' , error) if name == main ': main( ' credit_card_types. ssv' )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions