samedi 25 avril 2015

Python: How to track "correct" answer in randomized multiple choice

I successfully built a function to prompt the user with a question, followed by randomly ordered answer choices. However, since the answer choices are now randomized, how will python recognize the user input (a number: 1, 2, 3, or 4) for the "correct" answer?

import random

def answers():
    answerList = [answer1, answer2, answer3, correct]
    random.shuffle(answerList)
    numberList = ["1: ", "2: ", "3: ", "4: "]
    # A loop that will print numbers in consecutive order and answers in random order, side by side.
    for x,y in zip(numberList,answerList):
        print x,y 

# The question
prompt = "What is the average migrating speed of a laden swallow?"
#Incorrect answers
answer1 = "Gas or electric?"
answer2 = "Metric or English?"
answer3 = "Paper or plastic?"
# Correct answer
correct = "African or European?"

# run the stuff
print prompt
answers()

# Ask user for input; but how will the program know what number to associate with "correct"?
inp = raw_input(">>> ")




Aucun commentaire:

Enregistrer un commentaire