Back to Unit 4 Programming
Textbook Chapter 9.1
Data entered by users can not be trusted. There are two basic methods of trying to catch mistakes in user entered data:
Verification: Getting a user (or another system) to verify that the data entered is correct. E.g.
Validation: Automatic, but simple checks that can be done by the current program alone. E.g.,
user_name@domain.tld
E.g., New password input
while True:
password = input("Enter your new password: ")
check = input("Reenter your password to check: ")
if password == check:
break
print("Passwords do not match, please try again!")
# update password in database!
print("Great! Password is now updated")
See textbook for more examples (page 227)
The basic logic of such a verification check is easy, but it gets messier once you want to add user feedback.
REPEAT
INPUT password
INPUT check
UNTIL password = check
OUTPUT "Done!"
An exercises below is to rewrite using a WHILE loop.
E.g., Check user supplied age - see textbook page 229 for a different implementation
age = -1 # prime the loop
while age <= 0:
age_str = input("Enter your age in years: ")
if age_str.isdigit():
age = int(age_str)
if age > 0:
break
print("Age must be a positive integer")
print(f"{age} is a good age to be!")
A more modern / Pythonic approach would be to use Try-Catch to use the exception system to handle bad input. See e.,g., https://study.com/academy/lesson/data-validation-exception-handling-in-python.html
As pseudocode variables are not explicitly typed, it is hard to write pseudocode presence and type checks. However, range & length &c checks are still ok.
Here is a pseudocode equivalent
REPEAT
INPUT age
IF age <= 0
OUTPUT "Age should be a
positive whole number"
END IF
UNTIL age > 0
OUTPUT age
OUTPUT "is a good age to be!"
A user is signing up for a website. Write a program that will ask for a username, date of birth and email for the new account. Echo (repeat) them back to the user and ask them to confirm / verify the data to continue or restart. Loop until verified.
# Example Session
Please enter your preferred username: aisshark
Please enter your date of birth: 03/02/1993
Please enter your email address: ais@ais.edu.au
Is the following date correct:
username = aisshark, DOB = 03/02/1993, email = ais@ais.edu.au (Y/N)? N
Please enter your preferred username: aisshark
Please enter your date of birth: 03/02/1993
Please enter your email address: ais@ais.edu.sg
Is the following date correct:
username = aisshark, DOB = 03/02/1993, email = ais@ais.edu.sg (Y/N)? Y
Great! Your account is being set up :)
Rewrite the new password example from above to use a WHILE-DO-ENDWHILE loop (instead of a REPEAT-UNTIL loop). Here is the pseudocode outline, don't forget about the user prompts!
INPUT password, check
WHILE password <> check DO
INPUT password, check
ENDWHILE
A library card for a school library has to be 5 digits long and the last digit is a check digit that makes the sum of digits 0 modulo 10.
E.g.:
Write a program that will repeatedly ask for a library number until a valid one is given. You should check in order: Presence, Type, Length, Check Digit.
Write a program that given a number/string will check that it is a valid ISBN-10 or ISBN-13. Test it by getting the ISBNs of some IGCSE computer science books [Cambridge, Kinokuniya]. (Structured Version of Exercise)
Ask a user for their birthdate in the form dd/mm/yyyy. Use string slices and methods to confirm that is is of the correct format.
Bonus: Check that it is also a birthdate that is in the past!