Session 1 - Basic quiz game

Folllowing the steps given in the above worksheet we implemented a quiz that worked but
  • the sequecne of questions was fixed
  • the possibile answers were shown in a fixed order.
Both of these limitations makes it too easy to beat the quiz. Today we will try to fix these limitations. The completed programme is given here quiz_basic.py

Session 2 - Creating a Question Manager

To start, you should
  • Download the quiz_files.py and put it in your coderdojo_tramore/quiz_games directory. This file is like yout quiz_basic.py, all I have done is made the screen and the text boxes smaller.
  • Download the questions.zip and put it in your coderdojo_tramore/quiz_games directory. Then click on this zip file and extract its contents. This will create a subdirectory questions which will contain our questions, organised by topic.
We could use files to store questions in practically an infinite number of ways — each with different advantages and disadvantages. The structure proposed here is:
  • Store everything in a sub-directory called questions.
    This will help keep our code separate from our data — always a good idea.
  • Questions will be stored in text format files (not binary), so we can edit the files using text based editors like Notepad.
    Hence, we can open to read a file using open(filename) since the default mode is "rt" (i.e. reading text).
  • Questions will be grouped by topic. So will use a separate file per topic, and the name of the file will be based on the topic.
    The filename will be the topic (with spaces replaced by underscore "_") followed by its extension (.txt).
  • Within each topic file, the questions are stored as:
    • One question per line.
      This means we can use readlines() to read in all questions from a file and we get back a list of questions.
    • Question specified by giving five pieces of information (as strings) separated by a comma. The five peices of information are: question statement, correct answer, wrong answer 1, wrong answer 2, , wrong answer 3.
      We can use split(",") split a line containing a quesiton into its separate parts.
  • Finally we will store each question as a dictionary. This is a new concept for you but is well worth learning as it is a useful tool to store different type of data efficiently.
To help keep things organised (and us sane) we are going to split our code into two python files:
  • The file quiz_files.py will deal with everything concenred with running the quiz — drawing the screen, reacting to inputs, timing, etc.
  • The file question_manager.py will deal with reading (and writing) topics and questions.

    I have written an outline of the question manager which you can get from the above link or copying the code below.

    import glob
    
    
    def convert_filename_to_topic(filename):
        """Convert topic filname to topic title (for printing)."""
        
        return ""
    
    
    def list_topics():
        """Return list of available question topics."""
        
        return []
    
    
    def load_topic(topic):
        """Load all questions for the given topic."""
    
        return []
    
    
    def save_topic(topic, questions):
        """Save list of questions to the given topic."""
    
        return
    
    # code to run when testing
    if __name__ == "__main__":
    
        print("Testing question_manager ...\n")
        
        print("Getting list of topics ...")
        topics = list_topics()
        print(topics)

    Our first task is to implement the above functions and to test our implementation.

Updating our quiz to use the question manager

Once we have our question manager implemented and tested we need to update our quiz programe, quiz_files.py, so that it:
  • Runs over all topics in random order.
  • For each topic it pickes, say 5, random questions and askes the questions in random order.
  • For each question it prints the answer options in random order.
  • Player wins if they anwser all questions correctly within the give time limit for each question, otherwise they lose. This is not for the faint hearted!
We will do this next week ...

Session 3 - Using our question manager in our quiz

At the end of last week's session we completed a question manager. We will use that code today or download my copy as question_manager_working.py.

We need to update our quiz programe, quiz_files.py, so that it:

  • Runs over all topics in random order.
  • For each topic it pickes, say 5, random questions and askes the questions in random order.
  • For each question it prints the answer options in random order.
  • Player wins if they anwser all questions correctly within the give time limit for each question, otherwise they lose. This is not for the faint hearted!

Also, I want to show you all a nice online tool for exploring python data structures (lists, dictionaries, tuples, sets), called PythonTutor, in particular the Live programming mode where you see how the data is stired and the output as you type each line.

The Completed Program

The following links

contain the completed quiz program and our question manager. There are still some things that can be implemented, for example:
  • Include varying difficulty, by say, reducing the time to answer questions at the start of every topic.
  • Allow lives, so you can have up to, say, three incorrect answers. But for each incorrect answer your score halves.
  • Keep a track of how often a question is asked and how often it is answered correctly. This is harder and is something we might come back to at a later point.
Note that you will need to edit the question_mamanger.py file to change "questions/" to "questions\\" or better r"questions\" (notice the 'r' before the string to indicate that the backslash should be treated as a normal character.), if you are using windows. This is because OSX and linux use a forwardslash as our path separator while windows using backslash. It would be nice if we could have python code that would work regardless on what operating system we are using. There is. Have a look at os.path.sep.