Question

In: Computer Science

Textbook: Starting out with Python (3rd or 4the Edition) Question: Programming Exercise # 9 - Trivia...

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.

Solutions

Expert Solution

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:


Related Solutions

Starting out with Python 4th edition Chapter 2 Programming Exercise 12 Stock Transaction Program Not sure...
Starting out with Python 4th edition Chapter 2 Programming Exercise 12 Stock Transaction Program Not sure how to do this
Starting Out with Python 4th ed., Page 104 Programming exercise #14 When a bank account pays...
Starting Out with Python 4th ed., Page 104 Programming exercise #14 When a bank account pays compound interest, it pays interest not only on the principal amount that was deposited into the account, but also on the interest that has accumulated over time. Suppose you want to deposit some money into a savings account, and let the account earn compound interest for a certain number of years. The formula for calculating the balance of the account after a specified number...
Chapter 6, Starting out with Programming and Logic, 4th Edition, Page 265 #6 Kinetic Energy In...
Chapter 6, Starting out with Programming and Logic, 4th Edition, Page 265 #6 Kinetic Energy In physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object’s kinetic energy: KE=12mv2 The variables in the formula are as follows: KE is the kinetic energy, m is the object’s mass in kilograms, and v is the object’s velocity, in meters per second. Design a function named kineticEnergy that accepts...
Mubarak textbook Construction Project Scheduling and Control - 3rd edition(Chapter 11) can be used as reference,...
Mubarak textbook Construction Project Scheduling and Control - 3rd edition(Chapter 11) can be used as reference, but problem not from textbook PROBLEM 2 2. A project team developed a detailed construction plan that includes activities with uncertain durations, including precedence constraints. The plan was simulated 10 times, and using a significance level (α) of 0.05, the expected project duration was found to be: 365 ± 47 days In other words, the true mean duration could be anywhere between 318 days...
Python Programming 4th edition: Write a program that asks the user for the number of hours...
Python Programming 4th edition: Write a program that asks the user for the number of hours (float) and the pay rate (float) for employee pay role sheet. The program should display the gross pay with overtime if any and without overtime. Hints: Given base_hr as 40 and ovt_multiplier as1.5, calculate the gross pay with and Without overtime. The output should look like as follows: Enter the number of hours worked: Enter the hourly pay rate: The gross pay is $XXX.XX
This is an exercise to design and write a Python program in good programming style for...
This is an exercise to design and write a Python program in good programming style for a simulation of stock price over a period of 100 days. In this exercise, you are asked to simulate the stock price starting at $100.00 for 100 days with a daily fluctuation based on the Normal Distribution with mean = 0.0 & sigma = 0.0125. The program will show the daily stock price, the 7-day minimum, the 7-day maximum, the 7-day average, and the...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences of t in s to u, result stored in v * * Example: * * char v[100]; * replace("hello", "el", "abc", v) => v becomes "habclo" * replace("", "el", "abc", v) => v becomes "" (no change) * replace("hello", "abc", "def", v) => v becomes "hello" (no change) * replace("utilities", "ti", "def", v) => v becomes "udeflidefes" * */ void replace(char *s, char *t,...
Based on the textbook “Selling Building Partnerships" 10th edition Chapter 7's questions 8 and 9 on...
Based on the textbook “Selling Building Partnerships" 10th edition Chapter 7's questions 8 and 9 on page 194, Although there is no firm rule, explain what you think to be the best time of day as well as the worst time of day for calling the following individuals as a saleperson: "a) a college computer/bookstore manager (to sell computer accessories; b) a manager at a glass installation and repair company (to sell a new tool to remove broken glass shards);...
Question 29.6 on p. 982 of the textbook university physics 13th edition states that a magnet...
Question 29.6 on p. 982 of the textbook university physics 13th edition states that a magnet would reach terminal velocity even if there is no air resistance. How would air resistance change the situation? Is it significant?
Starting Out with Java (6th Edition) chapter 8, 3PC Requirement: Each of the RoomCarpet and RoomDimension...
Starting Out with Java (6th Edition) chapter 8, 3PC Requirement: Each of the RoomCarpet and RoomDimension classes should have a toString method, an equals method, a copy method, and a copy constructor. The RoomCarpet class should have mutators and accessors for both of its fields.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT