The purpose of today's lab is to practice using strings and reinforce the writing of functions. To this end, we will write a number of functions that deal with the validation of user inputed strings:*
Begin by creating a folder for this week's lab called lab07
and create a program file within that folder called validation.py.
Our plan for today is to create a menu driven application that allows the user to choose which kind of validation they would like to perform, and uses the option "Q" to quit. Since we will be showing this menu often, begin by writing a function that prints the menu, and then call this function as part of the basic interaction. This function's only job is to print the menu!! There should not be any input or logic in this function -- just print statements.
Start by downloading this starter code: validation.py. Note that the code is structured in the following manner:
Study the code and try it out, making sure that you understand the basic looping structure and the operations provided by the menu system.
We are now ready to add real functionality to our program (instead of just printing a nice message!).
A social security number has the format ddd-dd-dddd where
each of the d's indicate a digit between 0 and 9 (inclusive) and then
dashes. In this first exercise, we will add the option to our program
that prompts for a social security number, and then uses a function to
verify that the number given has the appropriate format.
Write a function called isValidSSN that receives
a string indicating a possible social security number, and returns
whether or not the string is properly formatted (that is, a boolean).
One helpful string method here is isdigit() but it may
be helpful to review other built-in functions available for strings
in your textbook, or in the str documentation available online: https://docs.python.org/3/library/stdtypes.html#string-methods
Remember, taking the time to write out an algorithm before you start coding is time well spent!
To test your function, add a social security validation option to
your menu function, as well as appropriate code to call isValidSSN
when desired by the user. Your program should use the return value of
the function to print either Valid SSN or Invalid
SSN as determined by the function.
Social security numbers have fixed length, and very strict rules about format. Let's try something a bit more complicated.
Requiring strong passwords is a basic security step for many applications and websites. In this exercise, we will write a function that verifies that a proposed password meets certain criteria.
Write a function called isValidPassword that checks
whether a given string is a valid password according
to the following rules:
To test your function, add a password validation option to your menu
function, as well as appropriate code to call isValidPassword
when desired by the user. Your program should use the return value of
the function to print either Valid Password or Invalid
Password as determined by the function.
Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with:
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered incorrectly (not whether the card is currently active or in good-standing). In addition to the length and first digit checks, the algorithm performs some numeric computations and verifies that the final result is divisible by 10. This algorithm is sometimes called the "Mod 10 check".
To actually implement the credit card number validity check will require several steps. Your solution must create each of the functions listed below, but how you implement them is up to you. That said, the input to each function must be a string:
Create a function called isValidPrefix
which checks the validity of the first character(s) of the card.
Note: If the first character(s) of the card is not one of those
listed above, this function should return False.
Create a function called sum_of_odds
that adds together all digits in the odd places from right to left
in the card number string. This function should return
this sum. For example:
sum_of_odds('4532503015077839')
Odd digits from right to left are 9, 8, 7, 5, 0, 0, 2, 5 whose sum is 36. NOTE: To do the sum, you will need to convert the characters to integers.
Create a function called sum_of_double_evens
that doubles every second digit from right-to-left. If doubling of
a digit results in a two-digit number, add up the digits to get a
single-digit number. Return the sum of all of the
single-digit numbers. For example:
sum_of_double_evens('4532503015077839')
Even digits from right-to-left are [3,7,0,1,3,5,3,4].
Doubling these gives [6,14,0,2,6,10,6,8]. Adding
digits of two-digit numbers gives [6,5,0,2,6,1,6,8],
the sum of which is 34, which should be returned.
Create a function called isValidCC
which receives a credit card number string cc, and
then checks each of the following:
cc is one of the valid options
cc is between 13 and 16 characters
cc has only numeric digit characterssum_of_odds(cc) + sum_of_double_evens(cc) is
divisible by 10
If cc satisfies each condition above then the function
should return True, otherwise False.
To test your isValidCC function, add a credit card
validation option to your menu function, as well as appropriate code
to call isValidCC when desired by the user. Your program
should use the return value of the function to print either Valid
Credit Card or Invalid Credit Card as determined by the
function.
Add the option to verify a credit card number to your program and test that it works as expected. (To find some examples of valid credit card numbers, you can look here: http://www.freeformatter.com/credit-card-number-generator-validator.html
Submit all the code and supporting files for the exercises in this lab. We will grade this exercise according to the following criteria:
Submit your solutions to these lab exercises using your appropriate submission environment.
If you worked with a partner, make sure you both have a copy of the files.
If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!
* These examples are based on similar examples from Y. Daniel Liang.