Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import string
- import random
- class Question:
- def __init__( self, prompt = "", answers = [], \
- correct_answer = None):
- self.prompt = prompt
- self.answers = answers
- self.correct_answer = correct_answer
- # If we are creating answers "in-line", use an index
- # to reference the correct answer
- if ( type(self.correct_answer) == type( int() ) ):
- self.correct_answer = self.answers[self.correct_answer]
- class Answer:
- def __init__( self, text = "", point_value = 0 ):
- self.text = text
- self.point_value = point_value # expect it in 1...10
- #------------------------------------------------
- class Quiz:
- def __init__( self ):
- self.score = 0
- def tell_current_score( self ):
- print "Your current score is %d." % self.score
- print '='*30 # ==============================
- print ""
- def ask_a_question( self, question ):
- answered = False
- print question.prompt
- number_of_possible_answers = len(question.answers)
- for index, answer in enumerate(question.answers):
- print string.lowercase[index] + ") ", answer.text
- while ( not answered ):
- their_answer = raw_input('> ')
- if (their_answer and their_answer in string.lowercase[:number_of_possible_answers]):
- index_of_answer = string.lowercase.index(their_answer)
- answer_they_give = question.answers[index_of_answer]
- # Optional feedback...
- if ( answer_they_give.point_value <= 5 ):
- print "Bad answer!"
- else:
- print "Good answer!"
- if ( answer_they_give == question.correct_answer ):
- print "CORRECT! You got it right!"
- print "Adding %d points to your score..." % question.correct_answer.point_value
- self.score += question.correct_answer.point_value
- else:
- print "INCORRECT! You got it wrong!"
- answered = True
- else:
- print "That is not a valid answer!"
- answered = False
- def ask_many_questions( self, questions ):
- for question in questions:
- self.ask_a_question( question )
- self.tell_current_score()
- def ask_random_sample_of_questions( self, questions, number ):
- random_sample_of_questions = []
- for i in range( number ):
- new_question = random.choice(questions)
- random_sample_of_questions.append(new_question)
- questions.remove(new_question)
- self.ask_many_questions(random_sample_of_questions)
- def run( self ):
- # -------------------------------
- # self.ask_many_questions(
- self.ask_random_sample_of_questions(
- [
- # List of question...
- Question( \
- prompt = "The current Prime Minister of Canada is Pierre Elliot Trudeau.",
- answers = [
- Answer("True", 0),
- Answer("False", 10),
- ],
- correct_answer = 1 # so "False" is the correct answer in this case.
- ),
- # more questions....
- Question( \
- prompt = "Who is the 45th president of the United States?",
- answers = [
- Answer("George W. Bush", 3),
- Answer("Barack Obama", 4),
- Answer("Donald Trump", 10),
- Answer("Abraham Lincon", 1),
- ],
- correct_answer = 2 # so "Donald Trump" is the correct answer in this case.
- ),
- Question( \
- prompt = "The Toronto Maple Leafs have won 13 Stanley Cups.",
- answers = [
- Answer("True", 10),
- Answer("False", 0),
- ],
- correct_answer = 0 # so "True" is the correct answer in this case.
- ),
- Question( \
- prompt = "What was the last year the Toronto Maple Leafs won the Stanley Cup?",
- answers = [
- Answer("1957", 6),
- Answer("1967", 10),
- Answer("1969", 3),
- Answer("2018", 1),
- ],
- correct_answer = 1 # so "1967" is the correct answer in this case.
- ),
- # ...
- ]
- , ) # only ask TWO random questions...
- if ( __name__ == "__main__" ):
- quiz = Quiz()
- quiz.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement