Skip to main content
  1. Teaching/
  2. teaching/

·3 mins

Credit Card Validation

When you enter a credit card number on most online websites, they can instantly tell whether the number is valid or not. How do they do this?

It turns out that they're not asking the credit card companies whether the card number is valid or not. They're running a simple algorithm on the credit card number to check if it's been entered correctly. In this assignment, you will implement a variant of this algorithm.

Assignments

Ultimately, you will only need to take in a credit card number and print whether it is valid or not. However, breaking this problem down into smaller pieces via functions will be very useful.

I recommend at least writing a function to turn a numeric string into an array of digits (e.g. to_int_array("2357") == [2, 3, 5, 7]).

The algorithm works as follows:

  1. Starting from the leftmost position, double every other digit.
  2. If doubling a digit results in a two-digit number, then add the two digits together. (For example, if the original digit is "8", we double it to get "16", then add them together to get "7")
  3. Add all the resulting numbers together
  4. Check if the result is divisible by 10.

If the result is divisible by 10, the number is valid. Otherwise, the number is invalid.

Examples

Here is an example execution on the number "22739" (Note: no credit card number is actually this short)

  1. Turn the string into an array of numbers [2,2,7,0,9]
  2. Starting from the 0-th position, double every other digit to get [4, 2, 14, 0, 18]
  3. Add the two digits together in the 2nd and 4th positions to get [4, 2, 5, 0, 9]
  4. Add all the numbers together to get 20
  5. The final number is divisible by 10, so this is a valid number.

Here are some other examples of valid and invalid numbers:

Valid:

  • 589820367083775
  • 931246195498579
  • 399312177329976
  • 593757865743391
  • 563766028258326
  • 6290050663349
  • 298680095396250
  • 306482437584277
  • 750624206570778
  • 878292797214523

Invalid:

  • 321682835363980
  • 876697276991007
  • 159343647589766
  • 783671862692297
  • 200681241295039
  • 401014019311978
  • 799850571634255
  • 816073496458361
  • 30051504474261
  • 104587374376205

Your program should ask the user for input and print out "valid" or "invalid" depending on the result.

Insight

Suppose you had a number which was invalid, but you wanted to modify it to pass this check. Describe a simple way of doing this.

For a single extra credit point, write a function that takes an invalid number and turns it into a valid one while changing as few digits as possible. (e.g. if your algorithm changes 3 digits, but it's possible to make it valid while only changing 2, the function is not right).

Submission

Submit a single file named CreditCards.py on Canvas. Your file needs to compile and run. It should also have a header with the following information (this goes in your source file, not in the program output):

# File: CreditCard.py
# Student: 
# Course: Intro to Programming
# 
# Date:
# Description of Program:
# Description of an algorithm to "fix" invalid numbers: