Boman Avong
7 min readJul 13, 2021

--

www.raspberrypi.org

Python Quiz Project

Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects. In this project, we will combine the basics of OOP and Python to build a simple quiz game.

Objectives:

1. Install Chocolatey

2. Install Python and Pycharm

3. Creating a Question Class

4. Creating the list of objects from data

5. QuizBrain and the next_question() method

6. Checking answers and keeping scores

7. How to continue showing new questions

8. Using Trivia DB to get news questions

Prerequisites:

  1. Windows or MAC OS. For the purpose of this project we will be working with Windows OS.

Project 4 Steps:

  1. Install Chocolatey

Chocolatey is software management automation for Windows that wraps installers, executables, ZIP files, and scripts into compiled packages. It’s modeled after Apt and yum, unlocks a new world of automatable and predictable package management to Microsoft’s operating system. Chocolatey has the largest online registry of Windows packages and aims to automate the entire software lifecycle from install through upgrade and removal on Windows operating systems.

a. Navigate to https://chocolatey.org/ and click on “Install Now” on the top right corner of the page.

Fig 1. Chocolatey Package Manager

b. On your Windows machine, open PowerShell in administrative mode. Copy and paste the command in the box shown below and press enter. Wait for the command to execute and complete.

If you don’t get any errors you are ready to install packages. Ensure you exit and reopen PowerShell (PS) before installing packages.

Fig 2. Install Link
Fig 3. Windows PowerShell Install

2. Install Python and Pycharm

You can skip this step if you already have Python and any other IDE of your choice. In our project I will be using Pycharm 2021.1 and Python 3.9.6.

On the main page, click on “Search Packages” and search for software packages you want to install. For Python, copy the command choco install python and paste into PS. Do the same for Pycharm and install. Notice that each package will have its own command.

Pycharm provides Intelligent Coding Assistance with code completion, code inspections, on-the-fly error highlighting, and quick-fix suggestions. Alternatively you can download directly from https://www.jetbrains.com/pycharm/.

Fig 4. Chocolatey Package Search

You can install multiple packages with the same command line. Simply add additional package names with a space in between as shown below for the same results as the above step.

Fig 5. Python and Pycharm Install

d. Now that we have Pycharm and Python installed, we can start building our code.

3. Creating a Question Class

In our project, we will be creating Classes. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.

a. Open Pycharm, click on “File” (Top left corner) and then click on “New Project”. Name your project under “Location” by editing the path and then click on “Create”(Bottom right). This will create the new project with the virtual environment. You should see a sample script under a file main.py by default.

Fig 6. Configuring Pycharm

Test your environment by clicking the “Run” button which is a green Triangle at the top right corner of the page. If successful, you should “Hi, Pycharm” executed at the bottom of the open window. Highlight sample code and delete.

Fig 7. Run sample code

b. To create a class, we will first create new files that will be used to define the classes. We accomplish this by opening Pycharm and clicking File (Top left)>New>File. Name the file question_model.py and then click enter. Repeat for data.py and quiz_brain.py. Once completed, these file should be empty. We will be modifying and adding codes to the files.

You should see the files created with the .py extension as shown below.

Fig 8. Create files for classes

c. In the file named question_model.py type the code below. In this step, we are generating an initial function for the class named Question. Here we generate a initial function for the class named Question. In this function, we hold our user input text as q_text and answer as q_answer

Fig 9. Create Question class

4. Creating the list of objects from data.py

In this step, we will be working on the main.py file and creating a list. Lists are one of the four built-in data structures in Python. They are used to store an ordered collection of items, which might be of different types but usually they aren’t. Commas separate the elements that are contained within a list and enclosed in square brackets.

The first step will be to create an empty list named question_bank. The value of text will be assigned to question_text and answer to question_answer respectively for any question in question_data.

The next step is to create a new value named new_question that holds question_text and question_answer. Finally the .append function will enable us add a new_question to the question_bank.

Fig 10. Create list

5. QuizBrain and the next_question()method:

Just as in the previous step, we will create another class called QuizBrain and set up the initial function. We will be creating two values self.question_number and self.score and assign both a value of 0. Lastly, we create a value named self.question_list and assign it with q_list.

In our previous main.py file add the following code below

quiz = QuizBrain(question_bank)

With this step, the question_bank list created in Step 4 will be mapped into QuizBrain and this also indicates that q_list is question_bank.

In Python, a method is a function that is available for a given object because of the object’s type. Now we tackle the next_question() method in the quiz_brain.py file.

Fig 11. Create method

In this function, we will create a value named current_question and assign self.question_list[self.question_number] as its value. This means that we are holding question_number in question_list to locate the current_question.

A counter is also implemented with self.question_number increasing by 1 each time. Then the user_answer will be assigned with input (f”Q.{self.question_number}: {current_question.text} (True/False): "). This code takes care of the user input.

The last line of code will use the values user_answer and current_question.answer to compare the correct answer values.

Fig 12. Create counter and compare values

6. How to continue showing new questions:

In this step, we will be utilizing a while loop in the main.py file. The loop will continue as long as self.question_number < len(self.question_list) is True.

In addition, question_number will increase with next_question() function until equals to len(self.question_list). If this condition is satisfied, the While loop will end.

To achieve this, we will be adding the following to the quiz_brain.py file and main.py file.

In quiz_brain.py file, we add the following code:

Fig 13. Generate new questions

and in the main.py file, we add the following code:

Fig 14. Generate ne questions 2

7. Checking answers and keeping scores

This final step compares answers and prints final score. We create a function named check_answer in the quiz_brain file. The function accepts user input and completes a comparison by comparing check_answer and user_answer.

If user_answer.lower() == correct_answer.lower():, our answer is correct and self.score which is the user score will be increased by 1. The output will display “You got it right!” else it will print “That is wrong”.

The final score will be printed using the self.score and self.question_number in the main.py file.

In this step, we will modify the quiz_brain.py file.

Fig 15. Checking answers and keeping scores

In this step, we will modify the main.py and append the below code at the very end.

Fig 16. Checking answers and keeping scores 2

Run the code and enjoy the game!!

8. Using Trivia DB to get news questions

You can modify the data.py file to add more questions by clicking this link here.

Final Python Codes

quiz_brain.py

question_model.py

main.py

data.py

References

https://www.datacamp.com/community/tutorials/18-most-common-python-list-questions-learn-python

https://medium.com/paul-zhao-projects/python-100-projects-in-100-days-learning-journal-ee23af9c1d80

--

--