Programming in Python to Make a Quiz about Linux & Bash

Object Oriented Programming (OOP) is a programming paradigm that deals with classes and objects. Classes are used as templates to declare and create objects, and an object is an instance of a class. Languages utilizing OOP include but aren’t limited to Java, JavaScript, C++, and Python.

In this exercise I used Python to build a Linux & Bash-themed quiz. The program calls upon users to answer ten questions, and at the end of the quiz, the user is told their score.

First I made the following dictionary named data.py (Image 1). This features ten trivia questions with answers about Linux & Bash. The source of these questions & answers are both from my own knowledge of commands, and some fun facts that I found on Google & Wikipedia.

Image 1: Question Data

I then needed to define the question class & parameters (Image 2). This defines the “question” → “answer” nature of the quiz.

Image 2: question-model.py

Note that as I go along in each file, I am running the small play button in the upper right hand corner of PyCharm (circled in yellow in Image 3). This button runs the written code and in the second, larger yellow circle from Image 3, it confirmed that there are no errors in the run.

Image 3: Testing the code as I go along

When I started the project in my IDE, PyCharm made a main.py file by default. At this point I needed to use that file as a place where information from other files funnel together.

Image 4 shows that:

  • On Line 1, I imported the question-data dictionary from question-data.py
  • On Line 2, I imported the question class from question-model.py
  • I left Line 3 blank
  • On Line 4, I wrote an empty question_bank and set it to an empty list
  • On Line 5, I ran a for loop. This cycles through my items in question-data.py executing each following action (which will be detailed in Line 6–9)
  • On Line 6, I called upon Python to pull a question from question-data.py
  • On Line 7, I called upon Python to pull an answer from question-data.py
  • On Line 8, I called upon Python to run the question_text & question_answer as parameters, setting the result to new_question.
  • On Line 9, I called upon Python to continue the cycle from Lines 6–8 by pulling a new question.
  • This “looping” will go through all questions and answers from question-data.py and add them to question_bank
Image 4: Starting main.py

I then created a file named quiz.py, and then created the quiz class. I set defaults of 0 for self.question_number and self.score. I then set the parameter q_list to self.question_list. This parameter is important because later in the process I called the Quiz class from our main.py file.

Image 5: Starting quiz.py

Next I created the next_question method for the Quiz class, so that the quiz will actually run through multiple questions. Here are the key lines as seen in Image 6:

  • On Line 8, I set current_question to call upon the question bank index number.
  • On Line 9, I added 1 to question_number so the questions will cycle through.
  • Line 10 takes the users answer.
  • Line 11 checks if the answer is correct.
Image 6: Adding next_question process to quiz.py

Then I added remaining_questions process. This will control the relationship between how many questions are remaining & how many questions are in the test.

Image 7: Adding remaining_questions process to quiz.py

Next I set up my check_answer process to check the answers and keep score. Some key details are:

  • On Line 18, I made it so that capitalizations are forgiven and the program reconciles if a lowercase answer is produced.
  • On Line 19 & 20, if the answer is correct the program prints “You are Correct!”.
  • On Line 21 & 22, if the two answers are not equal, then the program prints “Sorry, you are Incorrect”.
  • Then a string is printed indicating the right answer and current score.
  • Note the indentations of the last two prints. They are separate from the original if & else functions so that they are independent.
Image 8: Adding check_answer process to quiz.py

I then returned to my main.py file to perform the following:

On Line 3, I added from quiz import Quiz to actually pull the quiz into main.py (Image 9).

Image 9: Updating main.py

Then, at the bottom I added more code to continue showing new questions until we have answered all available questions:

  • On Line 11, I took the question_bank list in Quiz, then set that equal to quiz variable.
  • On line 13 & 14, a loop runs to ensure that the quiz continues rolling through questions as long as there are questions remaining.
  • On Line 16 & 17, the quiz concludes by printing “You’ve completed the quiz” and your final score.
Image 9: Concluding main.py

I then tested what I built by running main.py in PyCharm, which produced the first question to which I answered “True,” and that printed the result of the question and also printed the next question (Image 10).

Image 10: Starting the quiz

In Image 11, I answered more questions.

Image 11: About midway through the quiz

In Image 12, I completed the quiz & received my score.

Image 12: Finishing the quiz & receiving my score

Thankfully I received a 10/10 :)

Also to make absolute certain that my quiz was working outside of PyCharm, I went to my Terminal, changed directory into python-quiz, and ran the command python3 main.py. Indeed the program ran (Image 13).

Image 13: Confirming that the program also runs in Terminal

And for fun, here is the entire quiz and results taken by my fiance (Image 14).

Image 14: My fiance took the quiz and got a 7/10

Python can be a deep ocean but this project helps understand the fundamental nature of classes & objects. You can find the complete resources on my Github repo here, and check the credits at the end of this article for similar resources.

Thanks for reading.

Credits: This exercise was inspired by & guided by Python 100 projects in 100 days — Learning Journal by Paul Zhao, and also by fellow Level Up In Tech’ers Troy Ingram’s article from Python in Plain English + Danielle Wormley’s article.

Cloud & DevOps. Also Blockchain.