Learning Objectives
When your computer prorgam is asking questions to the end-user, the end-user may not answer these questions the way you expected them to. This may cause your program to not work properly. Ideally a good program should use some form of validation checks to ensure that the user is providing their answers in the correct format.
Let’s look at the following code that asks a simple Yes/No question:
answer = input("Are you over 16?") if answer == "Yes": print("You can take driving lessons to prepare for your driving test.") elif answer == "No": print("You are too young to learn how to drive a car.")
What would happen if:
- The user answers Yes ?
- The user answers No ?
- The user answers YES ?
- The user answers no ?
- The user answers Not yet ?
Now let’s look at how we could improve this code further:
Solution #1Explanations
answer = input("Are you over 16?").upper() if answer == "YES": print("You can take driving lessons to prepare for your driving test.") elif answer == "NO": print("You are too young to learn how to drive a car.")
Check the code, did you notice that we added .upper() at the end of line 1?
This will convert the user input into capital letters.
Then, within our if statements, we can compare the user input with “YES” and with “NO”.
This will convert the user input into capital letters.
Then, within our if statements, we can compare the user input with “YES” and with “NO”.
What do you think will now happen if:
- The user answers Yes ?
- The user answers No ?
- The user answers YES ?
- The user answers no ?
- The user answers Not yet ?
Solution #2Explanations
answer = input("Are you over 16?").upper() if answer == "YES": print("You can take driving lessons to prepare for your driving test.") elif answer == "NO": print("You are too young to learn how to drive a car.") else: print("Sorry your answer is not recognised. Restart the program and make sure you answer with the word Yes or the word No.")
By adding an else statement we can give some feedback to the end-user to tell them why their answer is not valid and how they need to answer the question next time
What do you think will now happen if:
- The user answers Yes ?
- The user answers No ?
- The user answers YES ?
- The user answers no ?
- The user answers Not yet ?
Solution #3Explanations
answer = "" while (answer!="YES" and answer!="NO"): answer = input("Are you over 16? (Yes or No)").upper() if answer == "YES": print("You can take driving lessons to prepare for your driving test.") elif answer == "NO": print("You are too young to learn how to drive a car.")
Using a while loop, we keep asking the question up until the user has typed a valid answer (Yes or No).
What do you think will now happen if:
What do you think will now happen if:
- The user answers Yes ?
- The user answers No ?
- The user answers YES ?
- The user answers no ?
- The user answers Not yet ?
Solution #4Explanations
def askYesNoQuestion(question): YesNoAnswer = "" while (YesNoAnswer!="YES" and YesNoAnswer!="NO"): YesNoAnswer = input(question).upper() return YesNoAnswer answer = askYesNoQuestion("Are you over 16? (Yes or No)") if answer == "YES": print("You can take driving lessons to prepare for your driving test.") elif answer == "NO": print("You are too young to learn how to drive a car.")
By creating our own askYesNoQuestion() function we can easily reuse this function every time we want to ask a YesNo question! In our main program, it is as easy as using an input command. It is however a lot more effective because it makes sure the user gives a valid answer!
Solution #5Explanations
def askYesNoQuestion(question): YesNoAnswer = input(question).upper() if YesNoAnswer == "YES" or YesNoAnswer == "NO": return YesNoAnswer else: return askYesNoQuestion(question) answer = askYesNoQuestion("Are you over 16? (Yes or No)") if answer == "YES": print("You can take driving lessons to prepare for your driving test.") elif answer == "NO": print("You are too young to learn how to drive a car.")
This time our askYesNoQuestion() function is not using a while loop but it is using recursion: It’s calling itself. Check on line 6 for the recursive call to itself. And it will keed doing so until it receives a valid answer.
Extension Task:
These different approaches to validate a user input only work for “Yes” / “No” questions.
Would you be able to adapt these to make them work for:
- Multiple answers questions? e.g. Which Science subject do you prefer: Biology, Physics, Chemistry or Computer Science?
- Number based questions? e.g. Which year group are you in: Only accept a value between 1 and 13. (Tip you will need to use < and > comparison operators to validate the user input.)