PDF download Download Article PDF download Download Article

If you want to learn Python, it is good to start with some simple text-based games. This article will show you how to make a quiz game.

  1. Install Python if you haven't already. There are two major versions of Python: Python 2 and Python 3. They are not interchangeable and support for Python 2 will end in 2020 [1] , so this article will assume that you install Python 3.
  2. Give it a descriptive name that ends with .py .
    Advertisement
  3. You will need it to randomly arrange the possible answers.
  4. A "QA" will consist of a question and a correct answer, and a list of possible other answers. The __init__() function will be called whenever you create a new QA object with the function QA() , with its arguments being passed on into the corresponding attribute. Type:
     class 
     QA 
     : 
     def 
     __init__ 
     ( 
     self 
     , 
     question 
     , 
     correctAnswer 
     , 
     otherAnswers 
     ): 
     self 
     . 
     question 
     = 
     question 
     self 
     . 
     corrAnsw 
     = 
     correctAnswer 
     self 
     . 
     otherAnsw 
     = 
     otherAnswers 
    
  5. Think of some questions with one right and some wrong answers. Create QA objects from each and put them in a list. Since the list will be quite long, you can break it across multiple lines for better readability. All this is done with following code (your text could be different):
     qaList 
     = 
     [ 
     QA 
     ( 
     "Where is Minsk?" 
     , 
     "in Belarus" 
     , 
     [ 
     "in Russia" 
     , 
     "such a city doesn't exist" 
     ]), 
     QA 
     ( 
     "What is the capital of Australia?" 
     , 
     "Canberra" 
     , 
     [ 
     "Sydney" 
     , 
     "New York" 
     , 
     "Australia doesn't exist" 
     ]), 
     QA 
     ( 
     "Which of the following is not on Earth?" 
     , 
     "Sea of Tranquility" 
     , 
     [ 
     "Mediterranean Sea" 
     , 
     "Baltic Sea" 
     , 
     "North Sea" 
     ]), 
     QA 
     ( 
     "Which of the following is not a continent?" 
     , 
     "Arctica" 
     , 
     [ 
     "Antarctica" 
     , 
     "America" 
     ]), 
     QA 
     ( 
     "Which of the following is not an African country?" 
     , 
     "Malaysia" 
     , 
     [ 
     "Madagascar" 
     , 
     "Djibouti" 
     , 
     "South Africa" 
     , 
     "Zimbabwe" 
     ])] 
    
  6. Set it to zero in the beginning. Add the line:
     corrCount 
     = 
     0 
    
  7. This will make your questions appear in a random order. This is done with following function:
     random 
     . 
     shuffle 
     ( 
     qaList 
     ) 
    
  8. The program should go over each item of the list and do something with it. Create such a loop with this expression:
     for 
     qaItem 
     in 
     qaList 
     : 
    
  9. After the loop expression, write the line:
     print 
     ( 
     qaItem 
     . 
     question 
     ) 
    
  10. They should appear in a random order so that the correct answer isn't always at the same position. Also, they should be prefaced by a number so that the user won't have to enter the entire answer again. Following code does this:
     print 
     ( 
     "Possible answers are:" 
     ) 
     possible 
     = 
     qaItem 
     . 
     otherAnsw 
     + 
     [ 
     qaItem 
     . 
     corrAnsw 
     ] 
     # square brackets turn correct answer into list for concatenation with other list 
     random 
     . 
     shuffle 
     ( 
     possible 
     ) 
     count 
     = 
     0 
     # list indexes start at 0 in python 
     while 
     count 
     < 
     len 
     ( 
     possible 
     ): 
     print 
     ( 
     str 
     ( 
     count 
     + 
     1 
     ) 
     + 
     ": " 
     + 
     possible 
     [ 
     count 
     ]) 
     count 
     += 
     1 
    
  11. First, tell the user that they have to enter the number of their. Then, check whether what the user entered is really a number that corresponds with an answer. If it isn't, tell the user again. Use this code (outside of the while loop you created for the output):
     print 
     ( 
     "Please enter the number of your answer:" 
     ) 
     userAnsw 
     = 
     input 
     () 
     while 
     not 
     userAnsw 
     . 
     isdigit 
     (): 
     print 
     ( 
     "That was not a number. Please enter the number of your answer:" 
     ) 
     userAnsw 
     = 
     input 
     () 
     userAnsw 
     = 
     int 
     ( 
     userAnsw 
     ) 
     while 
     not 
     ( 
     userAnsw 
     > 
     0 
     and 
     userAnsw 
     <= 
     len 
     ( 
     possible 
     )): 
     print 
     ( 
     "That number doesn't correspond to any answer. Please enter the number of your answer:" 
     ) 
     userAnsw 
     = 
     input 
     () 
    
  12. To do this, get the text of the answer for which the user entered a number and compare it with the text of the correct answer. If they are the same, the user's answer was correct and the variable corrCount should be increased by 1. Else, it's wrong and the user should be told the correct answer. Finally, print an empty line to add some space to the next question. Enter the following code:
     if 
     possible 
     [ 
     userAnsw 
     - 
     1 
     ] 
     == 
     qaItem 
     . 
     corrAnsw 
     : 
     print 
     ( 
     "Your answer was correct." 
     ) 
     corrCount 
     += 
     1 
     else 
     : 
     print 
     ( 
     "Your answer was wrong." 
     ) 
     print 
     ( 
     "Correct answer was: " 
     + 
     qaItem 
     . 
     corrAnsw 
     ) 
     print 
     ( 
     "" 
     ) 
    
  13. In the end, the user probably wants to know how many questions they got right. So tell them by adding the following expression outside the for-loop:
     print 
     ( 
     "You answered " 
     + 
     str 
     ( 
     corrCount 
     ) 
     + 
     " of " 
     + 
     str 
     ( 
     len 
     ( 
     qaList 
     )) 
     + 
     " questions correctly." 
     ) 
    
  14. Pay special attention to the indentation. Your complete code should look like this now:
     import 
     random 
     class 
     QA 
     : 
     def 
     __init__ 
     ( 
     self 
     , 
     question 
     , 
     correctAnswer 
     , 
     otherAnswers 
     ): 
     self 
     . 
     question 
     = 
     question 
     self 
     . 
     corrAnsw 
     = 
     correctAnswer 
     self 
     . 
     otherAnsw 
     = 
     otherAnswers 
     qaList 
     = 
     [ 
     QA 
     ( 
     "Where is Minsk?" 
     , 
     "in Belarus" 
     , 
     [ 
     "in Russia" 
     , 
     "such a city doesn't exist" 
     ]), 
     QA 
     ( 
     "What is the capital of Australia?" 
     , 
     "Canberra" 
     , 
     [ 
     "Sydney" 
     , 
     "New York" 
     , 
     "Australia doesn't exist" 
     ]), 
     QA 
     ( 
     "Which of the following is not on Earth?" 
     , 
     "Sea of Tranquility" 
     , 
     [ 
     "Mediterranean Sea" 
     , 
     "Baltic Sea" 
     , 
     "North Sea" 
     ]), 
     QA 
     ( 
     "Which of the following is not a continent?" 
     , 
     "Arctica" 
     , 
     [ 
     "Antarctica" 
     , 
     "America" 
     ]), 
     QA 
     ( 
     "Which of the following is not an African country?" 
     , 
     "Malaysia" 
     , 
     [ 
     "Madagascar" 
     , 
     "Djibouti" 
     , 
     "South Africa" 
     , 
     "Zimbabwe" 
     ])] 
     corrCount 
     = 
     0 
     random 
     . 
     shuffle 
     ( 
     qaList 
     ) 
     for 
     qaItem 
     in 
     qaList 
     : 
     print 
     ( 
     qaItem 
     . 
     question 
     ) 
     print 
     ( 
     "Possible answers are:" 
     ) 
     possible 
     = 
     qaItem 
     . 
     otherAnsw 
     + 
     [ 
     qaItem 
     . 
     corrAnsw 
     ] 
     # square brackets turn correct answer into list for concatenating with other list 
     random 
     . 
     shuffle 
     ( 
     possible 
     ) 
     count 
     = 
     0 
     # list indexes start at 0 in python 
     while 
     count 
     < 
     len 
     ( 
     possible 
     ): 
     print 
     ( 
     str 
     ( 
     count 
     + 
     1 
     ) 
     + 
     ": " 
     + 
     possible 
     [ 
     count 
     ]) 
     count 
     += 
     1 
     print 
     ( 
     "Please enter the number of your answer:" 
     ) 
     userAnsw 
     = 
     input 
     () 
     while 
     not 
     userAnsw 
     . 
     isdigit 
     (): 
     print 
     ( 
     "That was not a number. Please enter the number of your answer:" 
     ) 
     userAnsw 
     = 
     input 
     () 
     userAnsw 
     = 
     int 
     ( 
     userAnsw 
     ) 
     while 
     not 
     ( 
     userAnsw 
     > 
     0 
     and 
     userAnsw 
     <= 
     len 
     ( 
     possible 
     )): 
     print 
     ( 
     "That number doesn't correspond to any answer. Please enter the number of your answer:" 
     ) 
     userAnsw 
     = 
     input 
     () 
     if 
     possible 
     [ 
     userAnsw 
     - 
     1 
     ] 
     == 
     qaItem 
     . 
     corrAnsw 
     : 
     print 
     ( 
     "Your answer was correct." 
     ) 
     corrCount 
     += 
     1 
     else 
     : 
     print 
     ( 
     "Your answer was wrong." 
     ) 
     print 
     ( 
     "Correct answer was: " 
     + 
     qaItem 
     . 
     corrAnsw 
     ) 
     print 
     ( 
     "" 
     ) 
     print 
     ( 
     "You answered " 
     + 
     str 
     ( 
     corrCount 
     ) 
     + 
     " of " 
     + 
     str 
     ( 
     len 
     ( 
     qaList 
     )) 
     + 
     " questions correctly." 
     ) 
    
  15. If you're using an IDE, click on the "Run" symbol or menu item. If you're using a text editor, save your program, close the editor, and open your program with Python.
  16. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What to do if there are multiple correct answers?
    Invisible One
    Community Answer
    If that is the case, then do something like this: ``` correct answers = ['one', 'two', 'three'] answer = input("Name a number greater than zero but lower than four? ") if answer in answers: print("Answer correct! ") ```
Ask a Question
      Advertisement

      Tips

      • Watch out for correct indentation. Code inside a function or class definition is indented, as is code inside a loop. A Python program with wrong indentation will not work.
      • Press Ctrl + C if you want to interrupt the game and not finish it.
      • If you replace every call of input() with a call of raw_input() , this program will work in Python 2, but stop working in Python 3.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 82,647 times.

      Is this article up to date?

      Advertisement