How to design a simple quiz game in python

In this project we are going to design a simple quiz game using python class and instantiate an object with an instance method derived from python functions.

So step one let's define our class

class QuizGame:

And below our class let's state the variables we will need. So these will be;

  1. count set to -0. To count how many right answers we have so far, like so,
    count = 0
  2. set up our questions into a dictionary object, like so;
questions = {"What's the past tense of put? ": 'put', 'What is the capital of Iran? ': 'tehran',
            'What is the capital of jamaica? ': 'kingston', 'What is the highest mountain peak in the world? ': 'everest'}

      3. Now in step 3 we will setup our loop object to go through the questions and if the user inputs the right answer it should state whether the answer is right. If the answer typed by the user is wrong it should keep asking for the right answer. So that should look like so;

def question_game(self):
    print('Question: 1')
    for ques in self.questions:
        answer = input(ques).lower()
        while answer != self.questions[ques]:
            answer = input('Try again: ')
        else:
            print(f'{answer}! Great job!')
            self.count += 1
            print(f'You got {self.count}.')
    print(f'Great job! You got {self.count}/' + str(len(self.questions)))


if __name__ == '__main__':
    start = QuizGame()
    start.question_game()       

The above code simply defines the object name as question_game which takes 'self' parameter because it's under a class. It goes on to state 'Question 1' as the first question is printed. The for loop goes ahead to loop through the set of questions in the dictionary while taking an input from the user as an answer. We then go ahead to set a while loop if the answer isn't correct and asks the user to 'Try again'.

If the user gets it right it jumps out of the while loop and into the else clause.

So we then set start to the main class name and instantiate the object name question_game on the variable start we have set to QuizGame()

So in effect our entire code should look like this;

class QuizGame:
    count = 0
    questions = {"What's the past tense of put? ": 'put', 'What is the capital of Iran? ': 'tehran',
                'What is the capital of jamaica? ': 'kingston', 'What is the highest mountain peak in the world? ': 'everest'}

    def question_game(self):
        print('Question: 1')
        for ques in self.questions:
            answer = input(ques).lower()
            while answer != self.questions[ques]:
                answer = input('Try again: ')
            else:
                print(f'{answer}! Great job!')
                self.count += 1
                print(f'You got {self.count}.')
        print(f'Great job! You got {self.count}/' + str(len(self.questions)))


if __name__ == '__main__':
    start = QuizGame()
    start.question_game()





 

If you need assistance with your projects feel free to email me at info@airgad.com or whatsapp Jesse stay safe!