How to make a quiz game in python
Objective
In this article I'm going to be taking you through the steps of making a basic quiz game in python.
what is it going to do?
the game would ask the user if he/she would like to play, if yes, it would go on to ask a couple of questions and will calculate the number of questions the player answered correctly and give a score at the end of a game
Let's get started !!
Step 1
open your IDE or wherever you want to write your code, save your file as 'quiz.py' or something and we're good to go
step 2
firstly, let's write the code for confirmation, this will ask the user if he or she wants to play. If yes, the code goes unto the next line and if not the programme quits
choice = input('would you like to play? ')
if choice.lower() != 'yes':
quit()
what '.lower()' does is that it converts whatever case the user types the answer in to lower case so it would still recognise the answer even if our user types something like 'YeS'
step 3
Next, we have to declare score as '0' so we can then add a line of code later on that will add 1 to the score for each question the player gets correctly
score = 0
step 4
we can now ask the first question
answer1 = input('what year are we? ')
if answer1 != '2023':
print('you are wrong')
else:
score += 1
print('you got it right!!')
this block of code asks the user what year we are, and since we are currently in the year 202, it tells the user that he/she is wrong if the user inputs an answer different from that. And if not, that is, if the user inputs 2022 as an answer then it adds 1 to score and tells he/she that the answer is correct then it moves on to the next line
step 5
Here we can add another question, so we write a block of code similar to the previous one but we change the question and answer
answer2 = input('what is the name of this blog? ')
if answer2.title() != 'Iqmacodes':
print('you are wrong')
else:
score += 1
print('you got it right!!')
This block of code asks what the name of my blog is, and if the player inputs Iqmacodes but in another case like uppercase, it converts it to title case and tells the player that he/she got it right and adds 1 mark to 'score', else, it tells the user he/she is wrong.
step 6
Let's add one more question, let's say
answer3 = input("what is Iqma's favourite programming language? ")
if answer3.title() != 'Python':
print('you are wrong')
else:
score += 1
print('you got it right!!')
Now, I'll stop here with my questions but you can use this format to add as many questions as you want to your quiz
Step 7
Lastly, let's write the line that will print out the scores of the player
print('congratulations!!, you got ' + str(score) + ' questions correctly')
Another way to print this is to say
print('congratulations!!, you got', score, 'questions correctly')
Conclusion
After running this code you should get something like this in your terminal
I hope this article was helpful and I hope it was easy to follow along and understand for those new to python.
You should also try to do this on your own after reading the article and coding along with it.
Let me know how it goes in the comments, like and share if the article was helpful, see you in the next one...โ๐พ
Subscribe to my newsletter
Read articles from Crypt(iq) directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Crypt(iq)
Crypt(iq)
Exploring engineering and tech, storytelling my way through cybersecurity.