Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON: In this programming exercise, you will create a simple trivia game for t

ID: 3734097 • Letter: P

Question

PYTHON:

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 the 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 should also 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 question on the subject or subjects of your choice for the objects.

Explanation / Answer

Solution:

code:

class Question: #class definition

def __init__(self,q,op1,op2,op3,op4,ans): #constructor with parameters, q- the question, op- the options and ans the correct answer no.

self.q=q

self.op1=op1

self.op2=op2

self.op3=op3

self.op4=op4

self.ans=ans

def showQuestion(self):

print(self.q) #display the question

print("1.{0} 2.{1} 3.{2} 4.{3}".format(self.op1,self.op2,self.op3,self.op4))#display the options

def showResult(self,a): #show whether the answer chosen by the user is correct or not.

if a==self.ans:

result=1

print("Correct!")

else:

result=0

print("Incorrect. The correct answer is : ",self.ans)

return result #this return value will be added to the user's score

#main program

#creating the question bank

QuestionList=[] #create an empty list

#create first Question object and append it to the list

question="Who was the first astronaut to land on the moon?"

opt1="Yuri Gagarin"

opt2="Rocketman"

opt3="Neil Armstrong"

opt4="Elon Musk"

ans=3

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 2nd Question object and append it to the list

question="Which is the highest mountain in the world?"

opt1="Mt Everst"

opt2="Mt Etna"

opt3="Mt Moria"

opt4="Mt Killimanjaro"

ans=1

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 3rd Question object and append it to the list

question="Who was the first emperor of Rome?"

opt1="Julius Caesar"

opt2="Romulus"

opt3="Caesar Augustus"

opt4="Kaisar Willhelm"

ans=3

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 4th Question object and append it to the list

question="What is the name of the star closest to the Sun?"

opt1="Pole Star"

opt2="Proxima Centauri"

opt3="Andromeda"

opt4="Sirius"

ans=2

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 5th Question object and append it to the list

question="Who was te first President of the US?"

opt1="Thomas Beckett"

opt2="Barrack Obama"

opt3="Joseph Stalin"

opt4="George Washington"

ans=4

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 6th Question object and append it to the list

question="Which is the largest ocean in the world?"

opt1="Indian Ocean"

opt2="Arctic Ocean"

opt3="Pacific Ocean"

opt4="Atlantic Ocean"

ans=3

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 7th Question object and append it to the list

question="The capital of Australia is"

opt1="Canberra"

opt2="Sydney"

opt3="Ottawa"

opt4="Timbuktu"

ans=1

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 8th Question object and append it to the list

question="The Star Trek character, Mr Spock was from which planet?"

opt1="Vulcan"

opt2="Andoria"

opt3="PX-342"

opt4="Jupiter"

ans=1

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 9th Question object and append it to the list

question="Which coutry holds the record for deploying the highest number of satellites at a time?"

opt1="USA"

opt2="China"

opt3="Russia"

opt4="India"

ans=4

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#create 10th Question object and append it to the list

question="Which was the country to first manufacture a jet aircraft?"

opt1="USA"

opt2="Germany"

opt3="UK"

opt4="Japan"

ans=2

Q=Question(question,opt1,opt2,opt3,opt4,ans) #create a Question object, Q passing the question, options and answer no. as parameters to the constructor

QuestionList.append(Q) #append the question object to the list

#now start the trivia game

#initialize scores of both players to 0

Player1Score=0

Player2Score=0

print("The Trivia Game")

PlayerTurn=1 #this variable keeps track of which player's turn is going on. Start with 1, for player 1

for i in range(0,9):

print(" Player ",PlayerTurn," your question is :")

QuestionList[i].showQuestion()

n=int(input("Your Answer : "))

if PlayerTurn==1: #if this is the 1st player's turn

Player1Score=Player1Score+QuestionList[i].showResult(n)#display the result and add the points obtianed to the player's score

PlayerTurn=2 #next turn should be Player 2's

else: #if this is the 2nd player's turn

Player2Score=Player2Score+QuestionList[i].showResult(n)#display the result and add the points obtianed to the player's score

PlayerTurn=1 #next turn should be Player 1's

#Display the scores

if Player1Score>Player2Score:

print(" Congratulatons! Player 1 is the winner!")

elif Player1Score<Player2Score:

print(" Congratulatons! Player 2 is the winner!")

else: #both scores are equal

print("It's a Draw!")

print("The scores are:")

print("Player 1 : ",Player1Score)

print("Player 2 : ",Player2Score)