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.

For this exercise, I needed:

STEP 1: Question Data

Image 1: Question Data

STEP 2: Question Model

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

STEP 3: Test Bank

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

STEP 4: Quiz

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

STEP 5: Main Continued

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

TEST

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

CONCLUSION

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, Blockchain.