Advertisement
RootOfTheNull

Multiple Choice Questions

Nov 13th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import string
  4. import random
  5.  
  6.  
  7. class Question:
  8.  
  9.     def __init__( self, prompt = "", answers = [], \
  10.                   correct_answer = None):
  11.  
  12.         self.prompt = prompt
  13.         self.answers = answers
  14.  
  15.         self.correct_answer = correct_answer
  16.  
  17.         # If we are creating answers "in-line", use an index
  18.         # to reference the correct answer
  19.         if ( type(self.correct_answer) == type( int() ) ):
  20.             self.correct_answer = self.answers[self.correct_answer]
  21.  
  22.  
  23.  
  24. class Answer:
  25.  
  26.     def __init__( self, text = "", point_value = 0 ):
  27.  
  28.         self.text = text
  29.         self.point_value = point_value # expect it in 1...10
  30.  
  31.  
  32.  
  33. #------------------------------------------------
  34.  
  35.  
  36. class Quiz:
  37.  
  38.     def __init__( self ):
  39.  
  40.         self.score = 0
  41.  
  42.     def tell_current_score( self ):
  43.  
  44.         print "Your current score is %d." % self.score
  45.  
  46.         print '='*30 # ==============================
  47.         print ""
  48.  
  49.  
  50.  
  51.     def ask_a_question( self, question ):
  52.  
  53.         answered = False
  54.  
  55.         print question.prompt
  56.  
  57.         number_of_possible_answers = len(question.answers)
  58.  
  59.         for index, answer in enumerate(question.answers):
  60.             print string.lowercase[index] +  ") ", answer.text
  61.  
  62.         while ( not answered ):
  63.             their_answer = raw_input('> ')
  64.  
  65.             if (their_answer and their_answer in string.lowercase[:number_of_possible_answers]):
  66.  
  67.                 index_of_answer = string.lowercase.index(their_answer)
  68.                 answer_they_give = question.answers[index_of_answer]
  69.  
  70.                 # Optional feedback...
  71.                 if ( answer_they_give.point_value <= 5 ):
  72.                     print "Bad answer!"
  73.                 else:
  74.                     print "Good answer!"
  75.  
  76.  
  77.                
  78.                 if ( answer_they_give == question.correct_answer ):
  79.                    
  80.                     print "CORRECT! You got it right!"
  81.                     print "Adding %d points to your score..." % question.correct_answer.point_value
  82.                     self.score += question.correct_answer.point_value
  83.  
  84.  
  85.                 else:
  86.                     print "INCORRECT! You got it wrong!"
  87.                 answered = True
  88.  
  89.             else:
  90.                 print "That is not a valid answer!"
  91.                 answered = False
  92.  
  93.     def ask_many_questions( self, questions ):
  94.  
  95.         for question in questions:
  96.             self.ask_a_question( question )
  97.             self.tell_current_score()
  98.  
  99.     def ask_random_sample_of_questions( self, questions, number ):
  100.  
  101.         random_sample_of_questions = []
  102.  
  103.         for i in range( number ):
  104.             new_question = random.choice(questions)
  105.             random_sample_of_questions.append(new_question)
  106.             questions.remove(new_question)
  107.  
  108.         self.ask_many_questions(random_sample_of_questions)
  109.  
  110.  
  111.     def run( self ):
  112.  
  113.  
  114.         # -------------------------------
  115.  
  116.         # self.ask_many_questions(
  117.         self.ask_random_sample_of_questions(
  118.             [
  119.                 # List of question...
  120.  
  121.                 Question( \
  122. prompt = "The current Prime Minister of Canada is Pierre Elliot Trudeau.",
  123. answers =       [
  124.                     Answer("True", 0),
  125.                     Answer("False", 10),
  126.                     ],
  127. correct_answer = 1 # so "False" is the correct answer in this case.
  128.                 ),
  129.  
  130.                 # more questions....
  131.  
  132.                 Question( \
  133. prompt = "Who is the 45th president of the United States?",
  134. answers =       [
  135.                     Answer("George W. Bush", 3),
  136.                     Answer("Barack Obama", 4),
  137.                     Answer("Donald Trump", 10),
  138.                     Answer("Abraham Lincon", 1),
  139.                     ],
  140. correct_answer = 2 # so "Donald Trump" is the correct answer in this case.
  141.                 ),
  142.  
  143.  
  144.                 Question( \
  145. prompt = "The Toronto Maple Leafs have won 13 Stanley Cups.",
  146. answers =       [
  147.                     Answer("True", 10),
  148.                     Answer("False", 0),
  149.                     ],
  150. correct_answer = 0 # so "True" is the correct answer in this case.
  151.                 ),
  152.  
  153.  
  154.  
  155.                 Question( \
  156. prompt = "What was the last year the Toronto Maple Leafs won the Stanley Cup?",
  157. answers =       [
  158.                     Answer("1957", 6),
  159.                     Answer("1967", 10),
  160.                     Answer("1969", 3),
  161.                     Answer("2018", 1),
  162.                     ],
  163. correct_answer = 1 # so "1967" is the correct answer in this case.
  164.                 ),
  165.  
  166.                 # ...
  167.  
  168.             ]
  169.         , ) # only ask TWO random questions...
  170.  
  171.  
  172. if ( __name__ == "__main__" ):
  173.  
  174.     quiz = Quiz()
  175.     quiz.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement