Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
KBC game using Python
Kaun Banega Crorepati (KBC) is a popular Indian quiz show based on "Who Wants to Be a Millionaire." In this article, we'll create a simplified version of the KBC game using Python programming concepts like loops, conditionals, and user input.
What is KBC?
In the KBC game, contestants answer multiple-choice questions to win prize money that increases with each correct answer. The game continues until the player answers incorrectly or completes all questions successfully.
Key Components
Our KBC game implementation uses these Python concepts:
Variables ? Store questions, options, correct answers, and prize money
Lists and Dictionaries ? Organize question data efficiently
User Input ? Read player's answer choices using
input()Conditional Statements ? Check if answers are correct using
if/elseLoops ? Iterate through questions using
fororwhileloops
Algorithm
Initialize prize money to zero
Create a list of questions with options and correct answers
Display each question with multiple-choice options
Read and validate user's answer
Check if the answer matches the correct option
Update prize money for correct answers
End game on wrong answer or completion
Display final prize money earned
Method 1: Using For Loop
This approach iterates through all questions sequentially ?
import random
# Set initial prize money
prize_money = 0
# Create questions with options and correct 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 questions for variety
random.shuffle(questions)
# Iterate through questions
for question in questions:
print(f"\n{question['question']}")
for option in question["options"]:
print(option)
try:
user_answer = int(input("Enter your answer (1-4): "))
if user_answer == question["answer"]:
print("Correct answer!")
prize_money += 1000
print(f"Prize money: ?{prize_money}")
else:
print(f"Wrong answer! The correct answer was option {question['answer']}.")
break
except ValueError:
print("Invalid input! Game over.")
break
print(f"\nFinal Prize Money: ?{prize_money}")
Which planet is known as the Red Planet? 1. Jupiter 2. Venus 3. Mars 4. Saturn Enter your answer (1-4): 3 Correct answer! Prize money: ?1000 What is the capital of India? 1. Delhi 2. Mumbai 3. Kolkata 4. Chennai Enter your answer (1-4): 1 Correct answer! Prize money: ?2000 Who painted the Mona Lisa? 1. Vincent van Gogh 2. Pablo Picasso 3. Leonardo da Vinci 4. Claude Monet Enter your answer (1-4): 3 Correct answer! Prize money: ?3000 Final Prize Money: ?3000
Method 2: Using While Loop
This approach provides more control over the game flow and allows for additional features ?
# Set initial values
prize_money = 0
question_index = 0
game_over = False
# Create questions list
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
}
]
# Game loop
while not game_over and question_index < len(questions):
current_question = questions[question_index]
print(f"\nQuestion {question_index + 1}: {current_question['question']}")
for option in current_question["options"]:
print(option)
try:
user_answer = int(input("Enter your answer (1-4): "))
if user_answer == current_question["answer"]:
print("Correct answer!")
prize_money += 1000
print(f"Prize money: ?{prize_money}")
question_index += 1
else:
print(f"Wrong answer! The correct answer was option {current_question['answer']}.")
game_over = True
except ValueError:
print("Invalid input! Please enter a number between 1-4.")
game_over = True
# Check if player completed all questions
if question_index == len(questions):
print("\nCongratulations! You answered all questions correctly!")
print(f"Final Prize Money: ?{prize_money}")
Question 1: What is the capital of India? 1. Delhi 2. Mumbai 3. Kolkata 4. Chennai Enter your answer (1-4): 1 Correct answer! Prize money: ?1000 Question 2: Which planet is known as the Red Planet? 1. Jupiter 2. Venus 3. Mars 4. Saturn Enter your answer (1-4): 2 Wrong answer! The correct answer was option 3. Final Prize Money: ?1000
Comparison
| Method | Advantages | Best For |
|---|---|---|
| For Loop | Simple, clean code | Fixed number of questions |
| While Loop | More control, flexible conditions | Dynamic game flow, additional features |
Conclusion
Creating a KBC game in Python demonstrates fundamental programming concepts like loops, conditionals, and data structures. The for loop approach is simpler, while the while loop provides greater flexibility for game enhancements and user experience improvements.
