Trivial Lists


…And the first question is for you, Karl Marx. The Hammers – The Hammers is the nickname of what English football team? ‘The Hammers? (shot of Karl Marx furrowing his brow- obviously he hasn’t a clue) No? Well bad luck there, Karl. So we’ll go onto you Che. Che Guevara – Coventry City last won the FA Cup in what year? (cut to Che looking equally dumbfounded) No? I’ll throw it open. Coventry City last won the FA Cup in what year? (they all look blank) No? Well, I’m not surprised you didn’t get that. It was in fact a trick question. Coventry City have never won the FA Cup…

Having gone through some arduous work on the previous tutorial, this one will be a little gentler.  The task we’ve set ourselves this time is to write another game,  a trivia game.

Exercise:

Make a list of the things that a trivia game will need to do.
Don’t go further until you’ve done your list.

Finished?

No, honestly, do your list.
Ok, well, these are the sorts of things that I think a trivia game will need to do:

  1. it will need to have a list of questions
  2. for each question it will need to have a correct answer and at least one other incorrect answer, but possibily more.
  3. it will need to put the question to the user and get the person’s response
  4. it will also need to check to see if the response is correct
  5. optionally it will need to keep score

Given that each question needs to have a number of answers, we’re going to store them in a list.  We will store the question as the first element (ie at index number 0) in the list, and the second element (ie at index number 1) we will use to store the correct answer.  The rest of the list will be a number (1-4) of incorrect answers.  Here’s the list for our first question.

>>> question1 = ["What colour is Jango Fett's armour?",
... "Blue",
... "Green",
... "Red",
... "Pink, with lilac stripes and technicolour swirls"]
>>> question1
["What colour is Jango Fett's armour?", 'Blue', 'Green', 'Red', 'Pink, with lilac stripes and technicolour swirls']

Note that we’ve used double quotes so that the apostrophe in Fett’s prints properly.  Also see how we’ve created the list, by putting a comma in between each entry and with new entries on each line (you could put them all on one line, this is just a little easier to read).

Now let’s write a function to ask the question and get an answer:

>>> def askQuestion(questionList):
...      print questionList[0]
...      for i in range(len(questionList)-1):
...          print i,'. ',questionList[i+1]
...      answer = raw_input('Enter number of correct answer: ')
...      if answer == '0':
...          print 'Correct!'
...      else:
...          print 'Wrong'
...

So, we define a list which has the question, the correct answer and some wrong answers in it.  The list defines the question that the askQuestion() function asks for us.  The askQuestion() function also checks whether or not the answer is the correct one.  Note that what is tested is ‘0’, not 0.  That is, a string not a number.  This is because the raw_input() function returns a string, not a number and strings are not numbers.

Also of note here is the len() function, which is used inside the range() function.  When you run the len() function on an object len(object) it returns the length of the object.   In the case of a list, it is how many elements there are in the list.  Also, we need to skip over the first entry in the list (which has the question in it – entry 0), so we take the range up to one less than the length of the list, then add one when we print.  This starts printing at the second entry and ends a the last entry.

Let’s pass our question (question1) as an argument to the function (askQuestion()) and see how it works:

>>> askQuestion(question1)
What colour is Jango Fett's armour?
0 .  Blue
1 .  Green
2 .  Red
3 .  Pink, with lilac stripes and technicolour swirls
Enter number of correct answer: 0
Correct!
>>> askQuestion(question1)
What colour is Jango Fett's armour?
0 .  Blue
1 .  Green
2 .  Red
3 .  Pink, with lilac stripes and technicolour swirls
Enter number of correct answer: 2
Wrong

We can make new questions using the same format.  The way we’ve set up the function we can have any number of wrong answers:

question2 = ["Who was Obi's Jedi Master?",
"Qui-Gon Jinn",
"Scooby Doo",
"Darth Maul",
"Beowulf",
"Amadeus Mozart",
"Yoda"]

>>> askQuestion(question2)
Who was Obi's Jedi Master?
0 .  Qui-Gon Jinn
1 .  Scooby Doo
2 .  Darth Maul
3 .  Beowulf
4 .  Amadeus Mozart
5 .  Yoda
Enter number of correct answer: 0
Correct!
>>> askQuestion(question2)
Who was Obi's Jedi Master?
0 .  Qui-Gon Jinn
1 .  Scooby Doo
2 .  Darth Maul
3 .  Beowulf
4 .  Amadeus Mozart
5 .  Yoda
Enter number of correct answer: 4
Wrong

But there’s a problem with this structure.  Can you see what it is?

Exercise:

Check to see we’ve done numbers 1-4 of what we said we’d do.  Also, think about what is wrong with the askQuestion() function?  Why would this trivia game not be an interesting game? (hint: look at the correct answers)

How might we change this so that it can be used to ask a number of questions? (we’d probably change the way we store the questions, and write some more code to ask the additional questions)

About these ads

3 Responses to Trivial Lists

  1. Pingback: Trivia Game (Part 2) « Python Tutorials for Kids 8+

  2. Pingback: openSUSE Weekly News, Issue 140 is out! | Gnu Architecture

  3. Pingback: Dictionaries, Hovercraft, Eels « Python Tutorials for Kids 8+

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: