0% found this document useful (0 votes)
4 views7 pages

Import Threading

This document contains a Python script for a quiz game that allows multiple players to answer questions based on difficulty levels. It includes functionalities for input with a timeout, question retrieval, score tracking, and displaying results and reviews after the game. The game can be played repeatedly until the players choose to exit.

Uploaded by

kocherlabhavitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Import Threading

This document contains a Python script for a quiz game that allows multiple players to answer questions based on difficulty levels. It includes functionalities for input with a timeout, question retrieval, score tracking, and displaying results and reviews after the game. The game can be played repeatedly until the players choose to exit.

Uploaded by

kocherlabhavitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import threading

import random

# Input with timeout

def input_with_timeout(prompt, timeout):

user_input = [None]

def get_input():

user_input[0] = input(prompt)

thread = [Link](target=get_input)

[Link]()

[Link](timeout)

if thread.is_alive():

return None

return user_input[0]

def get_questions(level):

"""Return full question pool and time limit based on difficulty"""

if level == "1":

questions = [

{"question": "What is 2 + 2?", "options": ["A. 3", "B. 4", "C. 5", "D. 6"], "answer": "B"},

{"question": "What color is the sky?", "options": ["A. Blue", "B. Green", "C. Red", "D.
Yellow"], "answer": "A"},

{"question": "Which animal barks?", "options": ["A. Cat", "B. Dog", "C. Cow", "D.
Goat"], "answer": "B"},
{"question": "How many days in a week?", "options": ["A. 5", "B. 6", "C. 7", "D. 8"],
"answer": "C"},

{"question": "Sun rises in the?", "options": ["A. West", "B. North", "C. East", "D.
South"], "answer": "C"},

{"question": "Which is a fruit?", "options": ["A. Carrot", "B. Apple", "C. Potato", "D.
Onion"], "answer": "B"}

return questions, 15

elif level == "2":

questions = [

{"question": "Capital of India?", "options": ["A. Mumbai", "B. Delhi", "C. Kolkata", "D.
Chennai"], "answer": "B"},

{"question": "5 * 6 = ?", "options": ["A. 30", "B. 35", "C. 25", "D. 40"], "answer": "A"},

{"question": "Water freezes at?", "options": ["A. 0°C", "B. 50°C", "C. 100°C", "D.
10°C"], "answer": "A"},

{"question": "Largest planet?", "options": ["A. Earth", "B. Mars", "C. Jupiter", "D.
Venus"], "answer": "C"},

{"question": "HTML stands for?", "options": ["A. Hyper Trainer ML", "B. Hyper Text
Markup Language", "C. Hyper Text Machine Language", "D. None"], "answer": "B"},

{"question": "Which is a programming language?", "options": ["A. Python", "B.


Snake", "C. Tiger", "D. Lion"], "answer": "A"}

return questions, 10

elif level == "3":

questions = [

{"question": "Who developed Python?", "options": ["A. Dennis Ritchie", "B. Guido
van Rossum", "C. James Gosling", "D. Bjarne Stroustrup"], "answer": "B"},

{"question": "Square root of 144?", "options": ["A. 10", "B. 11", "C. 12", "D. 13"],
"answer": "C"},
{"question": "Binary of 5?", "options": ["A. 101", "B. 110", "C. 111", "D. 100"],
"answer": "A"},

{"question": "Fastest land animal?", "options": ["A. Lion", "B. Tiger", "C. Cheetah",
"D. Horse"], "answer": "C"},

{"question": "2^5 = ?", "options": ["A. 32", "B. 16", "C. 64", "D. 25"], "answer": "A"},

{"question": "RAM stands for?", "options": ["A. Random Access Memory", "B. Read
Access Memory", "C. Run Access Memory", "D. None"], "answer": "A"}

return questions, 5

else:

return [], 0

def play_player(name, questions, time_limit):

"""Run quiz for a player with given unique questions"""

print(f"\n {name}'s Turn\n")

score = 0

answers = []

for i, q in enumerate(questions, start=1):

print(f"{name} - Q{i}: {q['question']}")

for opt in q["options"]:

print(opt)

answer = input_with_timeout("Your answer (A/B/C/D): ", time_limit)

if answer is None:
print(" Time's up!")

[Link]("No Answer")

else:

answer = [Link]().upper()

[Link](answer)

if answer == q["answer"]:

print(" Correct!")

score += 1

else:

print(" Wrong!")

print()

return score, answers

def show_review(name, questions, answers):

print(f"\n {name}'s Review:\n")

for i, q in enumerate(questions):

print(f"Q{i+1}: {q['question']}")

print(f"Your: {answers[i]}")

print(f"Correct: {q['answer']}\n")

def run_quiz():

print("\n Quiz Game - Unique Questions Across Players")


# Players

try:

num_players = int(input("Enter number of players (2-4): "))

if num_players < 2 or num_players > 4:

print("Enter 2-4 players only.")

return

except:

print("Invalid input!")

return

players = [input(f"Enter Player {i+1} name: ") for i in range(num_players)]

# Difficulty

print("\nSelect Difficulty:")

print("1. Easy\n2. Medium\n3. Hard")

level = input("Enter choice: ").strip()

question_pool, time_limit = get_questions(level)

if not question_pool:

print("Invalid choice!")

return

# Number of questions per player

num_q = min(5, len(question_pool)//num_players)

if num_q == 0:

print("Not enough questions for all players!")

return
# Shuffle pool once

[Link](question_pool)

scores = {}

reviews = {}

start = 0

# Assign unique questions to each player

for player in players:

player_questions = question_pool[start:start+num_q]

start += num_q

score, answers = play_player(player, player_questions, time_limit)

scores[player] = score

reviews[player] = (player_questions, answers)

# Results

print("\n Final Scores:")

for p in players:

print(f"{p}: {scores[p]}")

max_score = max([Link]())

winners = [p for p in players if scores[p] == max_score]

if len(winners) == 1:

print(f" Winner: {winners[0]}")

else:
print(" Tie between:", ", ".join(winners))

# Show reviews

for p in players:

qs, ans = reviews[p]

show_review(p, qs, ans)

def main():

while True:

run_quiz()

if input("\n Play again? (yes/no): ").lower() not in ["yes", "y"]:

print(" Thanks for playing!")

break

if __name__ == "__main__":

main()

You might also like