0% found this document useful (0 votes)
2 views2 pages

Untitled Document

The document outlines a Python program for a quiz game involving multiple rounds of questions for players. Each player can answer questions in various formats, earning points based on their responses. The game concludes by displaying the final scores and announcing the winner.

Uploaded by

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

Untitled Document

The document outlines a Python program for a quiz game involving multiple rounds of questions for players. Each player can answer questions in various formats, earning points based on their responses. The game concludes by displaying the final scores and announcing the winner.

Uploaded by

amoahjeffrey582
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

import time

class Player:
def __init__(self, name, grade):
self.name = name
self.grade = grade
self.score = 0

def __str__(self):
return f"{self.name} (Grade {self.grade}) - Score: {self.score}"

class QuizGame:
def __init__(self):
self.players = []
self.rounds = [self.round1, self.round2, self.round3, self.round4, self.round5]

def add_player(self, name, grade):


self.players.append(Player(name, grade))

def ask_question(self, question, answer, time_limit):


print(f"\nQUESTION: {question}")
start_time = time.time()
try:
user_answer = input(f"Answer (within {time_limit} seconds): ")
except:
return False
elapsed = time.time() - start_time
if elapsed > time_limit:
print("Time's up!")
return False
return user_answer.strip().lower() == answer.strip().lower()

def round1(self, player):


print(f"\n--- ROUND 1: Basic Questions for {player.name} ---")
questions = [("What is 2 + 3?", "5"), ("Name the planet we live on", "earth")]
for q, a in random.sample(questions, k=len(questions)):
if self.ask_question(q, a, 60):
player.score += 1

def round2(self, player):


print(f"\n--- ROUND 2: Speed Race for {player.name} ---")
questions = [("What is 10 x 2?", "20"), ("Is 7 a prime number?", "yes")]
for q, a in random.sample(questions, k=len(questions)):
if self.ask_question(q, a, 30):
player.score += 1

def round3(self, player):


print(f"\n--- ROUND 3: Problem of the Day for {player.name} ---")
q, a = ("If a train travels 60 km in 1 hour, how far in 4 hours?", "240")
if self.ask_question(q, a, 240):
player.score += 3

def round4(self, player):


print(f"\n--- ROUND 4: True or False for {player.name} ---")
questions = [("The sun is a star. True or False?", "true"),
("Water boils at 50°C. True or False?", "false")]
for q, a in random.sample(questions, k=len(questions)):
if self.ask_question(q, a, 15):
player.score += 1

def round5(self, player):


print(f"\n--- ROUND 5: Riddles for {player.name} ---")
riddles = [("What has hands but can't clap?", "clock"),
("I speak without a mouth and hear without ears. What am I?", "echo")]
for q, a in random.sample(riddles, k=len(riddles)):
if self.ask_question(q, a, 60):
player.score += 2

def play_game(self):
for player in self.players:
for rnd in self.rounds:
rnd(player)

self.players.sort(key=lambda p: p.score, reverse=True)


print("\n--- FINAL SCORES ---")
for p in self.players:
print(p)
print(f"\nWinner: {self.players[0].name}! 🎉")

# --- Example usage ---


game = QuizGame()
game.add_player("Alice", 5)
game.add_player("Bob", 5)
game.play_game()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy