Yes or No!

yes-no

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:

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

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”.

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
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
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:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #4Explanations
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 make sure the user gives a valid answer!
Solution #5Explanations
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.)

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , , ,