Riddle me this – it's all Greek to me

Step 8 – about that answer

So far our program will tell the user that they got it right if they enter the word "man". Other than dealing with capital letters, that's the only answer it will accept.

But there are lots of possible correct answers:

And that's just for starters.

How on earth can we write an if – else block to deal with all of those possibilities?

* Sophocles was writing about 2,500 years ago. Women didn't have the same rights as men did in the world he lived in, so I guess he wouldn't have even thought to include woman as a possible answer. The world we live in is, fortunately, more equal, so we should include men, women and person as possible right answers.

As for Oedipus, he was mythical and so didn't exist.

The solution

The solution is to use a data structure called an array.

Imagine we have a program that wants the user to enter a colour of the rainbow. We know that there are seven correct answers because there are seven colours in a rainbow (I'm sure you remember the song).

The first thing to do is to set up an array with all of the answers in:

Code helper image

Notice that this uses square brackets to hold the items in the array. And each one is separated by a comma.

Then we use a clever version of the if line to check if the answer is in the array:

Code helper image

So long as the user spells the colour right, the program will allow any of the colours as a right answer. Clever, eh?

Using the solution in your riddle

See if you can use this idea in your program.

  1. Add an array with right answers in – make sure that this goes above the if – else block

    Call your array something like answerList

  2. Change the if line to be something like if answer in answerList:
  3. Run your program and check that everything works

How could you improve your program? What else could you add to it?