Is my credit card valid?
In this challenge we will use the Luhn Algorithm to
check if a debit card or credit card number is
a valid card number.
This method is used every time you scan or enter
your credit card number (e.g. when paying online) to
check if your card number is a valid credit card
number.
This method can be used to quickly detect credit card numbers that have been
mistyped or to detect when someone is trying to enter a fake/made up credit card
number. Note that it’s not 100% efficient, so is only used a as a pre-check to deter
some invalid card numbers. If this first test passes, other more robust checks will
be performed for the online transaction to be approved.
Python Workshop by Ebad ullah Qureshi 1
The Luhn
Algorithm
The Luhn Algorithm
consists of four key steps.
Python Workshop by Ebad ullah Qureshi 2
Exercise: Writing Functions
a) Write a function def convert_to_list(credit_card_number) that converts a
string of numbers to a list of numbers (use either map function or list comprehensions)
b) Write a function def luhn_double_digit(digit) that performs doubling of a digit
as per the Luhn Algorithm
c) Write a function def luhn_double(digits_list) that performs the function in (b)
on all the elements of an integer list
d) Write a function def check_credit_card(checksum) that decides whether a
credit/debit card is valid based on the value of the checksum
Python Workshop by Ebad ullah Qureshi 3
convert_to_list
luhn_double_digit
check_credit_cardluhn_double
is_my_credit_card_valid
Input Credit
Card number
Output Valid or
Invalid
sum
Exercise: Complete Function
Define a function def is_my_credit_card_valid(credit_card) that calls all the
functions defined in the previous section and outputs True or False depending on if the user’s
credit/debit card is valid or not.
Problem Solving by Bottom-up Approach
Python Workshop by Ebad ullah Qureshi 4
Testing our Function
Valid Invalid
Valid Invalid
Python Workshop by Ebad ullah Qureshi 5
Thank You!
Problem taken from Python101 challenges
Link to Problem: https://www.101computing.net/is-my-credit-card-valid/
Source code of solution to this Problem will be posted on Github
My Github Link: https://github.com/Ebad8931
Questions?
Python Workshop by Ebad ullah Qureshi 6

05 Is my Credit Card Valid Problem

  • 1.
    Is my creditcard valid? In this challenge we will use the Luhn Algorithm to check if a debit card or credit card number is a valid card number. This method is used every time you scan or enter your credit card number (e.g. when paying online) to check if your card number is a valid credit card number. This method can be used to quickly detect credit card numbers that have been mistyped or to detect when someone is trying to enter a fake/made up credit card number. Note that it’s not 100% efficient, so is only used a as a pre-check to deter some invalid card numbers. If this first test passes, other more robust checks will be performed for the online transaction to be approved. Python Workshop by Ebad ullah Qureshi 1
  • 2.
    The Luhn Algorithm The LuhnAlgorithm consists of four key steps. Python Workshop by Ebad ullah Qureshi 2
  • 3.
    Exercise: Writing Functions a)Write a function def convert_to_list(credit_card_number) that converts a string of numbers to a list of numbers (use either map function or list comprehensions) b) Write a function def luhn_double_digit(digit) that performs doubling of a digit as per the Luhn Algorithm c) Write a function def luhn_double(digits_list) that performs the function in (b) on all the elements of an integer list d) Write a function def check_credit_card(checksum) that decides whether a credit/debit card is valid based on the value of the checksum Python Workshop by Ebad ullah Qureshi 3
  • 4.
    convert_to_list luhn_double_digit check_credit_cardluhn_double is_my_credit_card_valid Input Credit Card number OutputValid or Invalid sum Exercise: Complete Function Define a function def is_my_credit_card_valid(credit_card) that calls all the functions defined in the previous section and outputs True or False depending on if the user’s credit/debit card is valid or not. Problem Solving by Bottom-up Approach Python Workshop by Ebad ullah Qureshi 4
  • 5.
    Testing our Function ValidInvalid Valid Invalid Python Workshop by Ebad ullah Qureshi 5
  • 6.
    Thank You! Problem takenfrom Python101 challenges Link to Problem: https://www.101computing.net/is-my-credit-card-valid/ Source code of solution to this Problem will be posted on Github My Github Link: https://github.com/Ebad8931 Questions? Python Workshop by Ebad ullah Qureshi 6