Make A Quiz Game In Python

In this article I will be talking about how you can easily make a Quiz Game in Python. I will revisit the basic concept of while loop in python and the quiz will be a direct implementation.

Pre-requisites

  1. Visual Studio 2013 or above
  2. Python interpreter installed

First of all let me recap the concept and syntax of while loop. Loops are generally used when you want to go ahead and run one event multiple times. For loop and while loop are the two most commonly used ones in python.

For loop is used generally when you have to repeat the number of events and you as a developer know the number of times you need to run through the loop to get your outcome. This is effective when you can predict the number of time(s) the loop should run.

While loop is one such loop which is effective when the developer has no idea about how many times the loop needs to run before he gets the desired value. I will use this sort of looping mechanism while developing a quiz because I never know how many tries the user will require to correctly answer the question.

Here is How I will approach the situation:

  1. answer ="0"  
  2. while answer != '4':  
  3. answer = input( "what is 2+2= ")  
  4. print ( "you are right")  
First I am initializing a variable “answer” with 0 . We should keep in mind that in python the input is always accepted in string format , so for easy understanding I’ve done all the assignments in str type only.

In the second line, I have given an condition which says , until the answer value is not equal to (!=) the right answer ,which in this case is 4 and we will keep on asking the user the “what is 2+2”.

In such a case where user types in the right answer , We will display “ you are right”,

code

Now let’s see how it looks in action. Just for fun and checking the consistency , I will enter few of the answers wrong and check if it gives me the question back again!

code

Thanks for the read. All the code is here.
 
Read more articles on Python:

 

Up Next
    Ebook Download
    View all
    Learn
    View all