Trivial Lists
September 6, 2010 3 Comments
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:
- it will need to have a list of questions
- for each question it will need to have a correct answer and at least one other incorrect answer, but possibily more.
- it will need to put the question to the user and get the person’s response
- it will also need to check to see if the response is correct
- 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)
Pingback: Trivia Game (Part 2) « Python Tutorials for Kids 8+
Pingback: openSUSE Weekly News, Issue 140 is out! | Gnu Architecture
Pingback: Dictionaries, Hovercraft, Eels « Python Tutorials for Kids 8+