SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Classes And Object Oriented Programming
Exercise:
Programming Exercises
Question:8 | ISBN:9780132576376 | Edition: 2

Question

Trivia Game
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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Trivia Game Program Code:

class Question:
    def __init__(self, question, answer1, answer2, answer3, answer4, correct_answer):
        self.question = question
        self.answer1 = answer1
        self.answer2 = answer2
        self.answer3 = answer3
        self.answer4 = answer4
        self.correct_answer = correct_answer

    def get_question(self):
        return self.question

    def get_answer1(self):
        return self.answer1

    def get_answer2(self):
        return self.answer2

    def get_answer3(self):
        return self.answer3

    def get_answer4(self):
        return self.answer4

    def get_correct_answer(self):
        return self.correct_answer

# Create a list of Question objects
questions = [
    Question("What is the capital of France?", "London", "Paris", "Berlin", "Madrid", 2),
    Question("Which planet is known as the Red Planet?", "Venus", "Mars", "Mercury", "Saturn", 2),
    Question("Who painted the Mona Lisa?", "Vincent van Gogh", "Leonardo da Vinci", "Pablo Picasso", "Michelangelo", 2),
    Question("What is the largest ocean in the world?", "Indian Ocean", "Arctic Ocean", "Atlantic Ocean", "Pacific Ocean", 4),
    Question("What is the chemical symbol for gold?", "Au", "Ag", "Fe", "Hg", 1),
    Question("Which country is known for its maple syrup?", "Canada", "Mexico", "Australia", "China", 1),
    Question("What is the tallest mountain in the world?", "Mount Kilimanjaro", "Mount Everest", "Mount Fuji", "Mount McKinley", 2),
    Question("Who is the author of the Harry Potter book series?", "J.K. Rowling", "Stephen King", "George R.R. Martin", "Dan Brown", 1),
    Question("Which animal is known as the 'King of the Jungle'?", "Tiger", "Elephant", "Lion", "Giraffe", 3),
    Question("What is the largest organ in the human body?", "Heart", "Liver", "Lungs", "Skin", 4)
]

player1_score = 0
player2_score = 0

# Trivia game loop
for i, question in enumerate(questions, 1):
    print("Question", i)
    print(question.get_question())
    print("1. ", question.get_answer1())
    print("2. ", question.get_answer2())
    print("3. ", question.get_answer3())
    print("4. ", question.get_answer4())
    
    player1_answer = int(input("Player 1, enter your answer (1-4): "))
    player2_answer = int(input("Player 2, enter your answer (1-4): "))
    
    if player1_answer == question.get_correct_answer():
        print("Player 1 answered correctly!")
        player1_score += 1
    else:
        print("Player 1 answered incorrectly!")
    
    if player2_answer == question.get_correct_answer():
        print("Player 2 answered correctly!")
        player2_score += 1
    else:
        print("Player 2 answered incorrectly!")

# Display final scores
print("Player 1 score:", player1_score)
print("Player 2 score:", player2_score)

# Determine the winner
if player1_score > player2_score:
    print("Player 1 wins!")
elif player2_score > player1_score:
    print("Player 2 wins!")
else:
    print("It's a tie!")

Executed Output:

Question 1
What is the capital of France?
1.  London
2.  Paris
3.  Berlin
4.  Madrid
Player 1, enter your answer (1-4): 2
Player 2, enter your answer (1-4): 1
Player 1 answered correctly!
Player 2 answered incorrectly!
Question 2
Which planet is known as the Red Planet?
1.  Venus
2.  Mars
3.  Mercury
4.  Saturn
Player 1, enter your answer (1-4): 3
Player 2, enter your answer (1-4): 4
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Question 3
Who painted the Mona Lisa?
1.  Vincent van Gogh
2.  Leonardo da Vinci
3.  Pablo Picasso
4.  Michelangelo
Player 1, enter your answer (1-4): 1
Player 2, enter your answer (1-4): 4
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Question 4
What is the largest ocean in the world?
1.  Indian Ocean
2.  Arctic Ocean
3.  Atlantic Ocean
4.  Pacific Ocean
Player 1, enter your answer (1-4): 3
Player 2, enter your answer (1-4): 3
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Question 5
What is the chemical symbol for gold?
1.  Au
2.  Ag
3.  Fe
4.  Hg
Player 1, enter your answer (1-4): 4
Player 2, enter your answer (1-4): 2
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Question 6
Which country is known for its maple syrup?
1.  Canada
2.  Mexico
3.  Australia
4.  China
Player 1, enter your answer (1-4): 2
Player 2, enter your answer (1-4): 4
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Question 7
What is the tallest mountain in the world?
1.  Mount Kilimanjaro
2.  Mount Everest
3.  Mount Fuji
4.  Mount McKinley
Player 1, enter your answer (1-4): 2
Player 2, enter your answer (1-4): 2
Player 1 answered correctly!
Player 2 answered correctly!
Question 8
Who is the author of the Harry Potter book series?
1.  J.K. Rowling
2.  Stephen King
3.  George R.R. Martin
4.  Dan Brown
Player 1, enter your answer (1-4): 2
Player 2, enter your answer (1-4): 4
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Question 9
Which animal is known as the 'King of the Jungle'?
1.  Tiger
2.  Elephant
3.  Lion
4.  Giraffe
Player 1, enter your answer (1-4): 3
Player 2, enter your answer (1-4): 3
Player 1 answered correctly!
Player 2 answered correctly!
Question 10
What is the largest organ in the human body?
1.  Heart
2.  Liver
3.  Lungs
4.  Skin
Player 1, enter your answer (1-4): 3
Player 2, enter your answer (1-4): 2
Player 1 answered incorrectly!
Player 2 answered incorrectly!
Player 1 score: 3
Player 2 score: 2
Player 1 wins!

 

0 0

Discussions

Post the discussion to improve the above solution.