Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Write a function credit(n, mode) that takes as input an integer n and an optional string argument mode. You can assume without checking that n

Write a function credit(n, mode) that takes as input an integer n and an optional string argument mode. You can assume without checking that n is an integer and mode is a valid string (if provided). The function is intended to help with the validation and generation of 16-digit credit card numbers. The first 15 digits of a typical credit card number identify the institution that issued the card and the individual account, and the final (right-most) digit is a check digit.

The validation of a credit card number, explained at an example, goes as follows:

  • Take, for example, the 16-digit credit card number 4578423013769219.
  • The check digit is the final 9 and we first process the first 15 digits 4-5-7-8-4-2-3-0-1-3-7-6-9-2-1.
  • Starting with the first digit, we multiply every second digit by two, resulting in 8-5-14-8-8-2-6-0-2-3-14-6-18-2-2.
  • Every time we have a two-digit number, we add those digits together for a one-digit result: 8-5-5-8-8-2-6-0-2-3-5-6-9-2-2.
  • We add up all the numbers: 8 + 5 + 5 + 8 + 8 + 2 + 6 + 0 + 2 + 3 + 5 + 6 + 9 + 2 + 2 = 71.
  • The credit card number is valid if and only if the computed sum plus the check digit is divisible by 10. In our example, 71 + 9 = 80 is divisible by 10. Hence this is a valid credit card number.

The function credit can do two things depending on the value of mode.

  • If mode has the string value verify, the function checks whether n corresponds to a valid credit card number by verifying that the check digit is correct, using the algorithm explained above. The function then returns the Boolean value True if the integer ncorresponds to a valid credit card number, and otherwise False.

  • If mode has the string value calculate, n should be a positive 15-digit integer and the function returns the single check digit as an integer. If n is not a positive 15-digit integer, then the function should return None.

  • If mode is not specified, the value verify should be assumed by default.

Step by Step Solution

3.39 Rating (171 Votes )

There are 3 Steps involved in it

Step: 1

Rawcode def creditn modeverify if n is negative or not a 16 digit number if n 0 or lenstrn 16 return ... 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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Cost Accounting A Managerial Emphasis

Authors: Charles T. Horngren, Srikant M.Dater, George Foster, Madhav

13th Edition

8120335643, 136126634, 978-0136126638

More Books

Students explore these related Accounting questions