Quiz Game with Python

In this article, I will take you through how we can create a quiz game with Python. If you are a beginner in Python then this is an ideal task for you because while building this quiz game you will cover almost every topic in Python.

Quiz Game with Python

Let’s get started with this task to build a quiz game with Python. We don’t need any package or module for this task as I will only use the basic python for building this quiz.

Also, Read – Machine Learning Full Course for free.

While thinking about a quiz, the first thing that comes in mind is options, now as a python programmer, I will build a function so that we can easily give the options as parameters while defining every new question. 

By defining the function for giving options it will save a lot of code in the process. So every time a problem hits you make a solution in your mind and then try to code as less as possible. This will build your ability to build algorithms.

Now let’s define a function to give options:

def give_options(x,y,z):
    print("a):", x)
    print("b):", y)
    print("c):", z)Code language: PHP (php)

Now I will pass a print statement to welcome us in the quiz game and I will take a user input that whether the user is ready or not. Then I will set the initial score as 0, and set the total number of questions as 4:

print("Hello! Welcome to my Quiz" "\n" "All Questions carries 10 marks each")
ans = input("Are you ready to play (yes/no): ")
a ="Note: wrtie answers! do not write option."
score = 0
total_questions = 4Code language: PHP (php)

Now as the questions are already in my mind so I will first create a list of correct answers so that we can verify the correct answers in the process of the quiz game by using the list indexing method in Python:

correct_ans =["python", "steve jobs", "artificial intelligence", "bitcoin"]Code language: JavaScript (javascript)

Now I will simply start with building questions for our quiz game, after defining every question I will use the function that I defined at the beginning to give the three options for the question.

After every answer whether correct or incorrect the user will be passed to the next question:

if ans.lower() == "yes":
    print(a)
    print("Question- What is the best Programming Language? ")
    give_options("Python", "C", "Java" )
    ans=input(">>>")
    if ans.lower() == correct_ans[0]:
        score=score+1
        print("Correct")
    else:
        print("Incorrect")
    print(a)
    print("Question- Who is the Founder of Apple Inc? ")
    give_options("Mark Zuckerberg", "Warren Buffet", "Steve jobs")
    ans = input(">>>")
    if ans.lower() == correct_ans[1]:
        score=score+1
        print("Correct")
    else:
        print("Incorrect")
    print(a)
    print("Question- What is more better among these? ")
    give_options("Data Science", "Artificial Intelligence", "Digital Marketing")
    ans = input(">>>")
    if ans.lower() == correct_ans[2]:
        score=score+1
        print("Correct")
    else:
        print("Incorrect")
    print(a)
    print("Question- What is the best Investment? ")
    give_options("Share Capital", "Mutual Funds", "Bitcoin")
    ans = input(">>>")
    if ans.lower() == correct_ans[3]:
        score=score+1
        print("Correct")
    else:
        print("Incorrect")Code language: PHP (php)

Now as we are done with the questions it’s time to show the scores to the user. I will multiply the score with 10 and then I will pass the if-else conditionals to print the status of the result of this Quiz game:

i = score*10
if i < 30:
    print("oops, your score is ",i,"/ 40 better luck next time.")
elif i ==30:
    print("wow! you scored ",i,"/ 40 you are quiet smart.")
else:
    print("Congratulations! it's a perfect ",i,"/ 40 you are Intelligent.")Code language: PHP (php)

Here is the output:

Hello! Welcome to my Quiz
All Questions carries 10 marks each
Are you ready to play (yes/no): yes
Note: wrtie answers! do not write option.
Question- What is the best Programming Language? 
a): Python
b): C
c): Java
>>>python
Correct
Note: wrtie answers! do not write option.
Question- Who is the Founder of Apple Inc? 
a): Mark Zuckerberg
b): Warren Buffet
c): Steve jobs
>>>steve jobs
Correct
Note: wrtie answers! do not write option.
Question- What is more better among these? 
a): Data Science
b): Artificial Intelligence
c): Digital Marketing
>>>data science
Incorrect
Note: wrtie answers! do not write option.
Question- What is the best Investment? 
a): Share Capital
b): Mutual Funds
c): Bitcoin
>>>bitcoin
Correct
wow! you scored  30 / 40 you are quiet smart.

Process finished with exit code 0

So this was a very basic quiz for Python beginners. Feel free to modify it. I hope you liked this article on quiz game with Python. Feel free to ask your valuable questions in the comments section below.

Follow Us:

Aman Kharwal
Aman Kharwal

Data Strategist at Statso. My aim is to decode data science for the real world in the most simple words.

Articles: 1609

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading