How to Make a Quiz Game in Python
How to Make a Quiz Game in Python
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.
Steps

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, so this article will assume that you install Python 3.

Open a new file in a text editor or your Python IDE. Give it a descriptive name that ends with .py.

Import the random library. You will need it to randomly arrange the possible answers.

Define a class "QA". 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

Create a list of QAs. 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"])]

Add a variable that counts how many answers were correct. Set it to zero in the beginning. Add the line:corrCount = 0

Shuffle the QA list. This will make your questions appear in a random order. This is done with following function:random.shuffle(qaList)

Make a loop. 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:

Output each question. After the loop expression, write the line: print(qaItem.question)

Output all possible answers. 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

Take the user's input. 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()

Check whether the user's answer was correct. 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("")

Output a conclusion. 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.")

Check your code. 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.")

Run the code. 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.

What's your reaction?

Comments

https://popochek.com/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!