
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
KBC Game Using Python
Introduction
A prominent Indian game show called Kaun Banega Crorepati (KBC) is modelled after the international game show "Who Wants to Be a Millionaire." In this post, we'll look at how to use the Python programming language to make a streamlined version of the KBC game.
KBC
Definition
In the KBC game, competitors select the proper answers to multiple-choice questions to win a prize that grows with each correct response. The objective of the game is to choose the right response from a collection of questions with several possible answers. A certain sum of money is awarded for each correct response, while an incorrect response causes the game to stop.
Syntax
Variables
question - Stores the question text. options - A list that contains multiple-choice options. answer - Stores the index of the correct answer.
User Input
input() - Reads user input from the console.
Conditional Statements
if - Checks a condition and executes a block of code if the condition is true. else - Executes a block of code if the preceding if statement condition is false.
Loops
for loop - Iterates over a sequence of elements. while loop - Repeats a block of code until a condition is met.
In the KBC game, variables are needed to store the question, the possible answers, and the right response. For the purpose of showing questions and approving user input, these variables are crucial.
User Input To read user input from the console, we employ the input() function. The player can then choose their response by keying in the matching option number.
Conditional Statements To compare the user's response to the right response, conditional statements like if and else are employed. The user receives a reward if their response is accurate. If not, the game is over.
Using loops, we can cycle through a list of questions and ask the player each one one at a time. While a while loop is advantageous when we wish to continue the game up until a certain condition is met, a for loop is effective when we have a set amount of questions.
Algorithm
Step 1 Set the initial amount of prize money.
Step 2 Create a list of questions with their options and correct answers.
Step 3 Repeat the list of questions.
Step 4 Display all of the questions and answers.
Step 5 Examine the user's response.
Step 6 Verify whether the user's response agrees with the right response.
Step 7 Adjust the prize money as necessary.
Step 8 If the user's response is inaccurate, the game is over.
Step 9 Till all questions are resolved or the game is over, repeat steps 3 as necessary.
Step 10 Display the final prize money earned by the player.
Approach
Approach 1 Using for loop.
Approach 2 : Using a while Loop
Approach 1: Using for Loop
Example
# Importing the random module to shuffle questions import random # Set the initial amount of prize money prize_money = 0 # Create a list of questions with options and answers questions = [{ "question": "What is the capital of India?", "options": ["1. Delhi", "2. Mumbai", "3. Kolkata", "4. Chennai"], "answer": 1 },{ "question": "Which planet is known as the Red Planet?", "options": ["1. Jupiter", "2. Venus", "3. Mars", "4. Saturn"], "answer": 3 },{ "question": "Who painted the Mona Lisa?", "options": ["1. Vincent van Gogh", "2. Pablo Picasso", "3. Leonardo da Vinci", "4. Claude Monet"], "answer": 3 }] # Shuffle the questions random.shuffle(questions) # Iterate over the questions using a for loop for question in questions: # Display the question and options print(question["question"]) for option in question["options"]: print(option) # Read the user's answer user_answer = int(input("Enter your answer (option number): ")) # Check if the user's answer is correct if user_answer == question["answer"]: print("Correct answer!") prize_money += 1000 else: print("Wrong answer!") break # End the game if the answer is incorrect # Display the final prize money earned by the player print("Prize Money:", prize_money)
Output
What is the capital of India? 1. Delhi 2. Mumbai 3. Kolkata 4. Chennai Enter your answer (option number): 1 Correct answer! Which planet is known as the Red Planet? 1. Jupiter 2. Venus 3. Mars 4. Saturn Enter your answer (option number): 2 Wrong answer! Prize Money: 1000
The random.shuffle() function is used to shuffle the questions in a different sequence each time the game is played.
Approach 2: Using a While Loop
Example
# Set the initial amount of prize money prize_money = 0 # Create a list of questions with options and answers questions = [{ "question": "What is the capital of India?", "options": ["1. Delhi", "2. Mumbai", "3. Kolkata", "4. Chennai"], "answer": 1 },{ "question": "Which planet is known as the Red Planet?", "options": ["1. Jupiter", "2. Venus", "3. Mars", "4. Saturn"], "answer": 3 },{ "question": "Who painted the Mona Lisa?", "options": ["1. Vincent van Gogh", "2. Pablo Picasso", "3. Leonardo da Vinci", "4. Claude Monet"], "answer": 3 }] # Set the initial question index question_index = 0 # Set the initial game status game_over = False # Game loop while not game_over: # Get the current question question = questions[question_index] # Display the question and options print(question["question"]) for option in question["options"]: print(option) # Read the user's answer user_answer = int(input("Enter your answer (option number): ")) # Check if the user's answer is correct if user_answer == question["answer"]: print("Correct answer!") prize_money += 1000 else: print("Wrong answer!") game_over = True # End the game if the answer is incorrect # Move to the next question question_index += 1 # Check if all questions have been answered if question_index == len(questions): game_over = True # Display the final prize money earned by the player print("Prize Money:", prize_money)
Output
What is the capital of India? 1. Delhi 2. Mumbai 3. Kolkata 4. Chennai Enter your answer (option number):1 Correct answer! Which planet is known as the Red Planet? 1. Jupiter 2. Venus 3. Mars 4. Saturn Enter your answer (option number): 3 Correct answer! Who painted the Mona Lisa? 1.Vincent van Gogh 2. Pablo Picasso 3. Leonardo da Vinci 4. Claude Monet Correct answer! Prize Money: 3000
The game loop, which runs continuously until the end of the game, is created using a while loop. If the user provides an erroneous response or if all questions have been answered, the game over condition is set to True.
Conclusion
In conclusion,  strong and adaptable foundation for developing enjoyable quiz game that let players test their knowledge and win cash in an entertaining and engaging way.