0% found this document useful (0 votes)
1 views

kbc

Computer project class 12 Free

Uploaded by

pandeyvansh299
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

kbc

Computer project class 12 Free

Uploaded by

pandeyvansh299
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Front page

Acknowledgement
certificate
Intriduction of project
Source Code

import mysql.connector

from mysql.connector import Error

# Function to create a connection to the database

def create_connection():

try:

connection = mysql.connector.connect(

host='localhost',

user='root', # Update with your MySQL username

password='12345', # Update with your MySQL password

database='kbc_game'

if connection.is_connected():

print("Connected to the database!")

return connection

except Error as e:

print(f"Error: {e}")

return None

# Function to fetch the questions from the database

def fetch_questions(connection):

cursor = connection.cursor()

query = "SELECT * FROM questions"

cursor.execute(query)

questions = cursor.fetchall()

return questions

# Function to ask questions and get user input

def ask_question(question, options):

print("\n" + question)
print("A) " + options[0])

print("B) " + options[1])

print("C) " + options[2])

print("D) " + options[3])

answer = input("Your answer (A/B/C/D): ").upper()

return answer

# Main game function

def play_game():

connection = create_connection()

if connection is None:

return

questions = fetch_questions(connection)

score = 0

question_number = 1

for question_data in questions:

question_id, question, option_a, option_b, option_c, option_d, correct_option = question_data

options = [option_a, option_b, option_c, option_d]

user_answer = ask_question(question, options)

if user_answer == correct_option:

print("Correct answer!")

score += 1

else:

print(f"Wrong answer! The correct answer is {correct_option}.")

print(f"Your current score is: {score}/{question_number}")


question_number += 1

print(f"\nGame Over! Your total score is: {score}/{len(questions)}")

connection.close()

# Entry point of the game

if __name__ == "__main__":

print("Welcome to Kaun Banega Crorepati!")

play_game()

You might also like