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.
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.
questions.
This will help keep our code separate from our data — always a good idea.
open(filename) since the default mode is "rt" (i.e. reading text).
.txt).
readlines() to read in all questions from a file and we get back a list of questions.
split(",") split a line containing a quesiton into its separate parts.
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.
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:
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 following links
contain the completed quiz program and our question manager. There are still some things that can be implemented, for example: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.