How to make KBC Quiz game in python?

How to make KBC Quiz game in python?

Introduction

Hi everyone, welcome to violet-cat-415996.hostingersite.com. In this tutorial, we are going to learn how to make a KBC Quiz game in python using simple lines of code. We are going to use random and time modules. We are making the lines of code easy to understand as we provide a detailed explanation of the code.

Import function

import random
import time

Here we are making use of the random and time modules. The random module helps in generating the random value i.e string, int, etc that we are using in our project. The time module provides a time-lapse function.

Writing the title of the quiz game in python

#print * 80 times
for i in range(80):
    print("*",end="")
    time.sleep(0)

print()
print("\t\t\t        Welcome to")
print("\t\t\tKaun Banega Crorepati")
for i in range(80):
    print("*",end="")
    time.sleep(0)
print()
a=input("\tEnter Your Name - ")
for i in range(80):
    print("*",end="")
    time.sleep(0)
print()

We are using the for loop function in this block which helps print the stars 80 times. The time.sleep(0) function helps to provide a time-lapse of 0 after each line of execution. We are printing the title of the quiz as “Welcome to Kaun Banega Crorepati”. As well as we are asking the user to enter the name, storing it in the variable a.

We are providing 15 questions that we are using in the quiz game. We are using the wronganswers and the answer variable for storing the incorrect answers and the correct answers.

Logic to select the questions randomly for the KBC Quiz game in python

Now, we are creating the logic to select the questions randomly from the list for the Kaun Banega Crorepati game

while True:
    while True:
        selectquestion=random.choice(questions)
        if selectquestion in attempquestion:
            pass
        elif selectquestion not in attempquestion:
            attempquestion.append(selectquestion)
            questionindex=questions.index(selectquestion)
            correctanswer=answer[questionindex]
            break

Here in this code block, we are performing the question selection while the condition satisfies, selectquestion is the variable that stores the question that is randomly selected from the list. If the question is in the attempt question then we will pass the if condition. Otherwise, if the select question is not in attemptquestion then we will append that question in the attemptquestion. The questionindex holds the selectquestion value. The correctanswer stores the answer to the question index and breaks the loop.

Logic to hold the answer and hold the count

optionslist=[]
inoptionlist=[]
optioncount=1
while optioncount<4:
    optionselection=random.choice(wronganswers[questionindex])
    if optionselection in inoptionlist:
        pass
    elif optionselection not in inoptionlist:
        optionslist.append(optionselection)
        inoptionlist.append(optionselection)
        optioncount+=1
optionslist.append(correctanswer)

We are creating two empty lists optionslist,inoptionlist. Option count, we have initialized to 1. While the option count is less than 4 we perform the following operation. optionselection variable stores the value of the randomly selected value of the wronganswers.

  1. If the option that we selected is in the optionlist then do pass.
  2. Otherwise, if the option we select is not inoptionlist. Append the value in both the lists optionlist and inoptionlist. Increase the option count by 1. Append the correctanswer to the optionslist.

Code for displaying questions

alreadydisplay=[]
optiontodisplay=[]
a1=True
while a1:
    a=random.choice(optionslist)
    if a in alreadydisplay:
        pass
    else:
        alreadydisplay.append(a)
        optiontodisplay.append(a)
        a1=not True
a1=True
while a1:
    b=random.choice(optionslist)
    if b in alreadydisplay:
        pass
    else:
        alreadydisplay.append(b)
        optiontodisplay.append(b)
        a1=not True
a1=True
while a1:
    c=random.choice(optionslist)
    if c in alreadydisplay:
        pass
    else:
        alreadydisplay.append(c)
        optiontodisplay.append(c)
        a1=not True
a1=True
while a1:
    d=random.choice(optionslist)
    if d in alreadydisplay:
        pass
    else:
        alreadydisplay.append(d)
        optiontodisplay.append(d)
        a1=not True

In this code block, we are creating 2 empty lists alreadydisplay and optiontodisplay . We have initialized a1== True. We are taking 4 variables i.e a,b,c, and d which store the random answers from the options list. If the variable’s value is in alreadydisplay list then it does nothing i.e pass.

Otherwise, it will append the values of the variables to alreadydisplay and optiontodisplay list and initialize a1==not True.

Code for checking the right answer

right_answer=""
if correctanswer==a:
    right_answer="a"
elif correctanswer==b:
    right_answer="b"
elif correctanswer==c:
    right_answer="c"
elif correctanswer==d:
    right_answer="d"
print("--------------------------------------------------------------------------------------------")
print("\t\t\tAmount Win - ",amount)
print("--------------------------------------------------------------------------------------------")
time.sleep(1)
print("\n\t\tQuestion ",count," on your Screen")
print("--------------------------------------------------------------------------------------------")
time.sleep(1)
print("  |  Question - ",selectquestion)
print("--------------------------------------------------------------------------------------------")
print("\t-----------------------------------------------------------------------------")
time.sleep(1)
print("\t|  A. ",a)
print("\t-----------------------------------------------------------------------------")
time.sleep(1)
print("\t|  B. ",b)
print("\t-----------------------------------------------------------------------------")
time.sleep(1)
print("\t|  C. ",c)
print("\t-----------------------------------------------------------------------------")
time.sleep(1)
print("\t|  D. ",d)
print("\t-----------------------------------------------------------------------------")

This code block initializes an empty variable as the right answer=””. We are using an if-else block which checks which is the right option from the list of a,b,c and d. If any of these options is correct it gets initialized to the right answer variable.

If the answer is the right answer it will display the amount you won. Display the next question along with the count and options for that particular question.

Logic to display the amount for each right answer

useranswer=input("\t\tEnter Correct Option\t   or \t press Q to quit.\n\t\t\t...").lower()
if useranswer==right_answer:
    if count==1:
        amount=1000
    elif count==2:
        amount=2000
    elif count==3:
        amount=5000
    elif count==4:
        amount=10000
    elif count==5:
        amount=40000
    elif count==6:
        amount=80000
    elif count==7:
        amount=160000
    elif count==8:
        amount=320000
    elif count==9:
        amount=640000
    elif count==10:
        amount=1500000
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        print("*********************************************************************************")
        print("\t\t\t \\\\\\\\\\ Congratulations! //////////")
        print("\t\t\t|||||||||| You Won The Game ||||||||||")
        print("*********************************************************************************")
        print("\n\n\t\t You Won Rs. ",amount)
        print()
        break
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
    print("*********************************************************************************")
    print("\t\t\t \\\\\\\\\\ Congratulations! //////////")
    print("\t\t\t|||||||||| Right Answer ||||||||||")
    print("*********************************************************************************")
    count+=1
elif useranswer=="q":
        print("\n\n\t\t You Won Rs. ",amount)
        break
else:
    print("*********************************************************************************")
    
    print("\t\t\tWrong Answer")
    print("*********************************************************************************")
    print("\n\n\t\t \tYou Won Rs. ",amount)
    print("*********************************************************************************")
    
    break

We are creating the variable useranswer which stores option we select from the option list. If the useranswer is equal to the right answer, the count of the question is increased by 1 and the amount allotted to that question is displayed. Once all the answers in the game are correct it will print “you won the game” and break the loop.

If the useranswer == “q” that is for quit, it quits the game. The system displays the amount won and breaks the loop, otherwise, the system displays “wrong answer” and the last amount you won.

Complete code for KBC Quiz game in python

#import random and time module
import random
import time
#print * 80 times
for i in range(80):
    print("*",end="")
    time.sleep(0)

print()
print("\t\t\t        Welcome to")
print("\t\t\tKaun Banega Crorepati")
for i in range(80):
    print("*",end="")
    time.sleep(0)
print()
a=input("\tEnter Your Name - ")
for i in range(80):
    print("*",end="")
    time.sleep(0)
print()
print("\n\t\tOK ",a," Let's Start The Game")
time.sleep(1)
questions=["Who is The Prime Minister of India","In Which Country Area 51 is Located","Which one is the largest Continent in the world","What is the Latest Version of Windows Since 2019","Which One of These Is not a Software Company","How Many MB Makes 1 GB","Facebook Was Firstly Developed By","Founder of Apple is","_________ is one of The Founder of Google","BIGG BOSS season 13 Starts in ____ & ends in _____","Apple's Laptop is Also Known as","First Apple Computer is Known as","Joystick is used For","____________ is used to Encrypt Drives in Computer"]
answer=["Narendra Modi","United States","Asia","Windows 10","Honda","1024","Mark Zuckenberg","Steve Jobs","Larry Page","2019 - 2020","Macbook","Mactonish","Playing Games","Bitlocker"]
wronganswers=[["Amit Shah","Aditya Nath Yogi","Azhar Ansari"],["India","Africa","Iraq"],["South Africa","North America","Europe"],["Windows 7","Windows 8","Windows 11"],["Oracle","Microsoft","Google"],["10024","1004","2024"],["Bill Gates","Larry Page","Azhar Ansari"],["Azhar Ansari","Charles Babbage","Sundar Pichai"],["Larry Hensberg","Sunder Pichai","Bill Gates"],["2020 - 2021","Not Starts Now","2018 - 2019"],["ThinBook","Notebook","ChromeBook"],["Apple v.1","Apple Computer","Appbook"],["Giving output command","Shutting down Computer","Log off Computer"],["KeyGuard","Windows Secure","No Software like this"]]
attempquestion=[]
count=1
amount=0
while True:
    while True:
        selectquestion=random.choice(questions)
        if selectquestion in attempquestion:
            pass
        elif selectquestion not in attempquestion:
            attempquestion.append(selectquestion)
            questionindex=questions.index(selectquestion)
            correctanswer=answer[questionindex]
            break
    optionslist=[]
    inoptionlist=[]
    optioncount=1
    while optioncount<4:
        optionselection=random.choice(wronganswers[questionindex])
        if optionselection in inoptionlist:
            pass
        elif optionselection not in inoptionlist:
            optionslist.append(optionselection)
            inoptionlist.append(optionselection)
            optioncount+=1
    optionslist.append(correctanswer)
    alreadydisplay=[]
    optiontodisplay=[]
    a1=True
    while a1:
        a=random.choice(optionslist)
        if a in alreadydisplay:
            pass
        else:
            alreadydisplay.append(a)
            optiontodisplay.append(a)
            a1=not True
    a1=True
    while a1:
        b=random.choice(optionslist)
        if b in alreadydisplay:
            pass
        else:
            alreadydisplay.append(b)
            optiontodisplay.append(b)
            a1=not True
    a1=True
    while a1:
        c=random.choice(optionslist)
        if c in alreadydisplay:
            pass
        else:
            alreadydisplay.append(c)
            optiontodisplay.append(c)
            a1=not True
    a1=True
    while a1:
        d=random.choice(optionslist)
        if d in alreadydisplay:
            pass
        else:
            alreadydisplay.append(d)
            optiontodisplay.append(d)
            a1=not True
    right_answer=""
    if correctanswer==a:
        right_answer="a"
    elif correctanswer==b:
        right_answer="b"
    elif correctanswer==c:
        right_answer="c"
    elif correctanswer==d:
        right_answer="d"
    print("--------------------------------------------------------------------------------------------")
    print("\t\t\tAmount Win - ",amount)
    print("--------------------------------------------------------------------------------------------")
    time.sleep(1)
    print("\n\t\tQuestion ",count," on your Screen")
    print("--------------------------------------------------------------------------------------------")
    time.sleep(1)
    print("  |  Question - ",selectquestion)
    print("--------------------------------------------------------------------------------------------")
    print("\t-----------------------------------------------------------------------------")
    time.sleep(1)
    print("\t|  A. ",a)
    print("\t-----------------------------------------------------------------------------")
    time.sleep(1)
    print("\t|  B. ",b)
    print("\t-----------------------------------------------------------------------------")
    time.sleep(1)
    print("\t|  C. ",c)
    print("\t-----------------------------------------------------------------------------")
    time.sleep(1)
    print("\t|  D. ",d)
    print("\t-----------------------------------------------------------------------------")
    useranswer=input("\t\tEnter Correct Option\t   or \t press Q to quit.\n\t\t\t...").lower()
    if useranswer==right_answer:
        if count==1:
            amount=1000
        elif count==2:
            amount=2000
        elif count==3:
            amount=5000
        elif count==4:
            amount=10000
        elif count==5:
            amount=40000
        elif count==6:
            amount=80000
        elif count==7:
            amount=160000
        elif count==8:
            amount=320000
        elif count==9:
            amount=640000
        elif count==10:
            amount=1500000
            print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
            print("*********************************************************************************")
            print("\t\t\t \\\\\\\\\\ Congratulations! //////////")
            print("\t\t\t|||||||||| You Won The Game ||||||||||")
            print("*********************************************************************************")
            print("\n\n\t\t You Won Rs. ",amount)
            print()
            break
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        print("*********************************************************************************")
        print("\t\t\t \\\\\\\\\\ Congratulations! //////////")
        print("\t\t\t|||||||||| Right Answer ||||||||||")
        print("*********************************************************************************")
        count+=1
    elif useranswer=="q":
            print("\n\n\t\t You Won Rs. ",amount)
            break
    else:
        print("*********************************************************************************")
        print("\t\t\tWrong Answer")
        print("*********************************************************************************")
        print("\n\n\t\t \tYou Won Rs. ",amount)
        print("*********************************************************************************")

        break

Output

Output of KBC Quiz game in python

Hence, we have successfully created the KBC Quiz game in python.

For more articles and projects on python, keep vising our website www.violet-cat-415996.hostingersite.com

Thank you for reading this article.


Also Read:

Share:

Author: Ayush Purawr