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


i built function prompt user question, followed randomly ordered answer choices. however, since answer choices randomized, how python recognize user input (a number: 1, 2, 3, or 4) "correct" answer?

import random  def answers():     answerlist = [answer1, answer2, answer3, correct]     random.shuffle(answerlist)     numberlist = ["1: ", "2: ", "3: ", "4: "]     # loop print numbers in consecutive order , answers in random order, side side.     x,y in zip(numberlist,answerlist):         print x,y   # question prompt = "what average migrating speed of laden swallow?" #incorrect answers answer1 = "gas or electric?" answer2 = "metric or english?" answer3 = "paper or plastic?" # correct answer correct = "african or european?"  # run stuff print prompt answers()  # ask user input; how program know number associate "correct"? inp = raw_input(">>> ") 

you shuffle list of answers, shouldn't, want shuffling list of indices... example of implementation, clarify mean, is

def ask(question, wrong, correct):     """ask: asks multiple choice question, randomizing possible answers.  question: string containing question wrong:    list of strings, each 1 possible answer, of them wrong correct:  list of strings, each 1 possible answer, of them correct  returns true if user gives 1 of correct answers, false otherwise."""      random import shuffle      # add operator (+) concatenates lists, [a,b]+[c,d]->[a,b,c,d]     answers = wrong + correct      # we'll need lengths of lists, let compute lengths     lw = len(wrong)     lc = len(correct)      # indices random list containing integers 0 lw+lc-1     indices = range(lw+lc) ; shuffle(indices)      # print question     print question      # print possible choices, using order of randomized list     # nb enumerate([1,3,0,2]) -> [(0,1),(1,3),(2,0),(3,2)]     #    i=0, index=1, next i=1, index=3, etc     i, index in enumerate(indices):         # print progressive number , possible answer,         # i+1 because humans prefer enumerations starting 1...         print "  %2d:\t%s" % (i+1, answers[index])      # repeatedly ask user make choice, until answer     # integer in correct range     ask = "give me number (%d-%d) of correct answer: "%(1,lw+lc)     while true:         n = raw_input(ask)         try:             n = int(n)             if 0<n<lw+lc+1: break         except valueerror:             pass         print 'your choice ("%s") invalid'%n      # @ point have valid choice, remains check if     # choice correct...     return true if indices[n-1] in range(lw,lw+lc) else false 

i forgot mention, example supports multiple correct answers...

you call this,

in [2]: q = "what third letter of alphabet?"  in [3]: w = [c c in "abdefghij"] ; c = ["c"]  in [4]: print "correct!" if ask(q, w, c) else "no way..." third letter of alphabet?    1:   d    2:   f    3:      4:   c    5:   h    6:   b    7:      8:   e    9:   g   10:   j give me number (1-10) of correct answer: c choice ("c") invalid give me number (1-10) of correct answer: 11 choice ("11") invalid give me number (1-10) of correct answer: 2 no way...  in [5]: print "correct!" if ask(q, w, c) else "no way..." third letter of alphabet?    1:   j    2:      3:   e    4:   h    5:      6:   d    7:   b    8:   c    9:   f   10:   g give me number (1-10) of correct answer: 8 correct! 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -