Reputation: 2306
I've been studying the following code to make a multiple choice question program with Python. I'm looking for some advice on how to increase the complexity of the program.
The current program contains the questions and answer sets within the python script. How can I recall the questions and answers from a separate file, preferably a spreadsheet, or also a text file?
The current program randomizes the order of all questions and randomizes two wrong answers per question. How can I assign particular wrong answers to a given question, but randomized the order in which answers are presented? (e.g., if recalled from a spreadsheet, column 1 contains the question, column 2 contains the correct answer, and columns 3-5 contain wrong answers. The program presents the question followed by all possible answers in a randomized order)
Thank you and mahalo.
import random
import os
# dictionary containing the information for the questions & answers
word_drills = {'class': 'Tell Python to make a new kind of thing.',
'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
'instance': 'What you get when you tell Python to create a class.',
'def': 'How you define a function inside a class.',
'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
'attribute': 'A property classes have that are from composition and are usually variables.',
'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'}
# Main portion of the program
def main():
right_answer_total = 0
wrong_answer_total = 0
percentage = 0.0
while True:
print_stats(right_answer_total, wrong_answer_total, percentage)
possible_answers = random.sample(word_drills, 3)
# User is presented with a question. A value from the previous randomly selected possible_answers is selected as the 'correct_answer'
correct_answer = random.choice(possible_answers)
question = word_drills[correct_answer]
print "Question: ", question
print "\n\n(a)%s (b)%s (c)%s" % tuple(possible_answers)
selection = raw_input("> ")
if selection not in ('a', 'b', 'c'):
print "That is not a valid selection."
break
answer = possible_answers[ord(selection) - ord('a')]
if answer == correct_answer:
print "That's correct!"
right_answer_total += 1
else:
print "I'm sorry, that is incorrect..."
wrong_answer_total += 1
percentage = 100 * right_answer_total / float(right_answer_total + wrong_answer_total)
# Stat tracking
def print_stats(right_answer_total, wrong_answer_total, percentage):
os.system('cls' if os.name=='nt' else 'clear')
print "-" * 37
print "| Stat Tracking |"
print "-" * 37
print "| Correct | Incorrect | Percentage |"
print "-" * 37
print "| %d | %d | %d %% |" % (right_answer_total, wrong_answer_total, percentage)
print "-" * 37
print "\n\n\n"
if __name__ == '__main__':
main()
Source: https://codereview.stackexchange.com/questions/14838/multiple-choice-quiz-with-stat-tracking
First post. Apologies for any forum faux paus. I will do better.
Upvotes: 2
Views: 263
Reputation: 6575
Take a look at random.choice
Example:
from random import choice
word_drills = {
'class': 'Tell Python to make a new kind of thing.',
'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
'instance': 'What you get when you tell Python to create a class.',
'def': 'How you define a function inside a class.',
'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
'attribute': 'A property classes have that are from composition and are usually variables.',
'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'
}
selected = word_drills[choice(word_drills.keys())]
Selecting 3 of 10 choices randomly
keys = word_drills.keys()
choices = []
for k in range(3):
select = choice(keys)
if not select in choices:
choices.append(select)
Upvotes: 1