Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This program is right on the borderline between being acceptable as a list/dict of lists and being better off written with classes. If there's any possibility that it will grow, you'd probably want to reorganize it as an object oriented program (OOP), like this:</p> <pre><code>import sys import random class Question(object): def __init__(self, question, answer, options): self.question = question self.answer = answer self.options = options def ask(self): print self.question + "?" for n, option in enumerate(self.options): print "%d) %s" % (n + 1, option) response = int(sys.stdin.readline().strip()) # answers are integers if response == self.answer: print "CORRECT" else: print "wrong" questions = [ Question("How many legs on a horse", 4, ["one", "two", "three", "four", "five"]), Question("How many wheels on a bicycle", 2, ["one", "two", "three", "twenty-six"]), # more verbose formatting Question(question="What colour is a swan in Australia", answer=1, options=["black", "white", "pink"]), # the last one can have a comma, too ] random.shuffle(questions) # randomizes the order of the questions for question in questions: question.ask() </code></pre> <p>That puts the logic of how the data structure is used closer to where the data structure is defined. Also (addressing your original question), it's easy to associate questions with answers because they're no longer in separate lists.</p> <p>There's no incentive not to do this because Python doesn't require much extra work to make a class. This is approximately the same length as the dict-of-lists solution (on which it was based), and is arguably easier to read, closer to you had in mind before you sat down to start programming. I often use classes even in four-line programs: OOP doesn't have to be a big deal.</p> <p><strong>Edit:</strong> You can manipulate the list of class instances the same way you'd manipulate a list of integers or strings. For example,</p> <pre><code>del questions[0] </code></pre> <p>removes the first item from the list,</p> <pre><code>questions.append(Question("What is a new question", 1, ["This is."])) </code></pre> <p>adds a new question to the end of the list, <code>pop</code> lets you use the list as a stack, etc. (See a Python tutorial for more.) If you want to remove specific questions using list's <code>remove</code> method, rather than removing it by index (what <code>del</code> does), then you'll need to tell Python what it means for two questions to be the same.</p> <p>Try adding these two methods to the class:</p> <pre><code> def __eq__(self, other): return self.question == other.question def __repr__(self): return "&lt;Question: %s? at %0x&gt;" % (self.question, id(self)) </code></pre> <p>The first defines two <code>Question</code> instances to be equal if their question strings are the same. You could extend the definition by also requiring their answers to be the same, but you get the idea. It allows you to do this:</p> <pre><code>questions.remove(Question("How many legs on a horse", 0, [])) </code></pre> <p>to remove the legs-on-a-horse question.</p> <p>The second pretty-prints the question objects on the Python command-line, so that you can see what you're doing when you experiment with these commands. The best way to learn is by trial-and-error, and this makes trial-and-error more productive.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload