In: Computer Science
Textbook: Starting out with Python (3rd or 4the Edition)
Question: Programming Exercise # 9 - Trivia Question
In this programming exercise, you will create a simple trivia
game for two players. The program
will work like this:
Starting with player 1, each player gets a turn at answering 5
trivia questions. (There
should be a total of 10 questions.) When a question is displayed, 4
possible answers are
also displayed. Only one of the answers is correct, and if the
player selects the correct
answer, he or she earns a point.
After answers have been selected for all the questions, the program
displays the number of
points earned by each player and declares the player with the
highest number of points the
winner.
To create this program, write a Question class to hold the data
for a trivia question. The
Question class should have attributes for the following data:
A trivia question
Possible answer 1
Possible answer 2
Possible answer 3
Possible answer 4
The number of the correct answer (1, 2, 3, or 4)
The Question class also should have an appropriate _ _init_ _
method, accessors, and mutators.
The program should have a list or a dictionary containing 10
Question objects, one for each
trivia question. Make up your own trivia questions on the subject
or subjects of your choice for
the objects.
CODE:
# data used to create the questions object dictionary
data={
"Q1":{
"title":"Tallest mountain in the world?",
"options":["Mount everest","Mount K2","Mount olympus","Mount batten"],
"answer":0
},
"Q2":{
"title":"Largest country in the world, by land area?",
"options":["USA","Russia","Canada","Australia"],
"answer":1
},
"Q3":{
"title":"Faster Train system in the world?",
"options":["US Railway","Shanghai Maglex","Japan - Bullet","TGV France"],
"answer":1
},
"Q4":{
"title":"Tallest building in the world?",
"options":["Mount everest","Taipaei 101","Buj Khalifa","Petonus"],
"answer":2
},
"Q5":{
"title":"Fastest Production motorcycle?",
"options":["Ducatti 999","BMW 1000R","Ninaj H2 R","Mount everest"],
"answer":2
},
"Q6":{
"title":"Tallest mountain in the world?",
"options":["Mount K2","Mount olympus","Mount everest","Mount batten"],
"answer":2
},
"Q7":{
"title":"Tallest mountain in the world?",
"options":["Mount everest","Mount K2","Mount olympus","Mount batten"],
"answer":0
},
"Q8":{
"title":"Tallest mountain in the world?",
"options":["Mount everest","Mount K2","Mount olympus","Mount batten"],
"answer":0
},
"Q9":{
"title":"Tallest mountain in the world?",
"options":["Mount K2","Mount everest","Mount olympus","Mount batten"],
"answer":1
},
"Q10":{
"title":"Tallest mountain in the world?",
"options":["Mount everest","Mount K2","Mount olympus","Mount batten"],
"answer":0
}
}
# class for the question object
class Question:
# initialization function
def __init__(self,title):
self.title=title
self.options=[]
self.index=-1
#mutator methods
# set the options
def set_options(self,options):
self.options=options
# set the answer index
def set_answer(self,index):
self.index=index
#accessor methods
# get the title
def get_title(self):
return self.title
# get the options array
def get_options(self):
return self.options
# get the answer index
def get_answer(self):
return self.index
# dictionay to hold the question obejct dictionary
dict_questions={}
for key,item in data.items():
# create the question object
tempObj=Question(item['title'])
tempObj.set_options(item['options'])
tempObj.set_answer(item['answer'])
dict_questions[key]=tempObj
# dictionary to hold the player 1 and player 2 scores
player_dict={
"1":0,
"2":0
}
# instruction message
print("Welcome to the trivia application")
print("Turn by turn questions would be asked by Player one and Player two")
print("10 points for correct answer, 0 for incorrect one")
print("Player scoring maximum as the end of 10th question would be the winner!")
print("For question answer by selection option 1-4, any other input would be treated as incorrect sanaswer")
print("Let's being")
# loop over the question object and aget answers
for key,value in dict_questions.items():
# print title
print("{}: {}".format(key,value.get_title()))
# print options
print("Options are: {}".format("\n".join(value.get_options()[0:])))
for key in player_dict.keys():
tempOpt=input("Player: {} Please input your answer".format(key))
# match if correct answer was provided
if (int(tempOpt.strip())-1)==value.get_answer():
player_dict[key]+=10
# check who is the winner and display
if player_dict['1']>player_dict['2']:
print("Player One is the winner with: {} points".format(player_dict['1']))
else:
print("Player Two is the winner with: {} points".format(player_dict['2']))
SNIPPET:
RESULT: