Question: i have codes but there is so many errors ! the client cannot write the answer, the server do not wait before start the game
i have codes but there is so many errors
the client cannot write the answer, the server do not wait before start the game the scores doesnot showed after every question and the client should enter the ip and the port numbers of the server then their names and if you find any another errors please fixed it :import socket
import time
import threading
class TriviaClient:
def initself serverip serverport, username:
self.serverip serverip
self.serverport serverport
self.username username
self.clientsocket socket.socketsocketAFINET, socket.SOCKDGRAM
self.clientsocket.settimeout
def connecttoserverself:
self.clientsocket.sendtoselfusername.encodeselfserverip self.serverport
printfConnected to selfserverip:selfserverport as selfusername
def receivemessagesself:
while True:
try:
data, self.clientsocket.recvfrom
message data.decode
printfServer: message
if "Question:" in message:
self.askquestionmessage
elif "Game Over" in message:
printGame Over. Disconnecting."
break
except socket.timeout:
continue
def askquestionself questiontext:
printfQuestion received: questiontext
answer inputYour answer or type 'quit' to exit:
if answer.lower "quit":
printExiting game."
self.closeconnection
exit
self.clientsocket.sendtoanswerencodeselfserverip self.serverport
def closeconnectionself:
self.clientsocket.close
printConnection closed."
def startclient:
username inputEnter your name:
client TriviaClientlocalhost username
client.connecttoserver
client.receivemessages
if namemain:
startclient
# Trivia questions database
questionsdb
What is the capital of France?", "Paris"
What is
Who wrote To Kill a Mockingbird'?", "Harper Lee"
What is the largest planet in our solar system?", "Jupiter"
What is the square root of
Who painted the Mona Lisa?", "Leonardo da Vinci"
What is the speed of light?", ms
Who discovered penicillin?", "Alexander Fleming"
What is the chemical symbol for gold?", Au
What is the tallest mountain on Earth?", "Mount Everest"
class TriviaServer:
def initself host'localhost', port:
self.host host
self.port port
self.clients # Stores client addresses and their names
self.scores # Stores client scores
self.currentround
self.maxrounds
self.questionsperround
self.serversocket socket.socketsocketAFINET, socket.SOCKDGRAM
self.serversocket.settimeout # Prevent blocking
try:
self.serversocket.bindselfhost, self.port
printfServer started on selfhost:selfport
except Exception as e:
printfError binding server to selfhost:selfport: e
exitdef startgameself:
# Wait for players to join
printWaiting for players to join..."
while lenselfclients: # Minimum players required
self.collectnewplayers
time.sleep # Avoid busy waiting
printfPlayers connected: joinselfclients.values
printGame is starting!"
self.broadcastmessageThe game is starting!"
# Start game rounds
while self.currentround self.maxrounds:
self.currentround
printf
Starting round selfcurrentround
self.broadcastmessagefRound selfcurrentround is starting! Get ready!"
self.askquestions
self.endround
time.sleep # Pause between rounds
printGame Over. Final scores:"
self.showscores def collectnewplayersself:
Collect new players who send their names."""
try:
data, addr self.serversocket.recvfrom
if addr not in self.clients:
self.clientsaddr data.decode
self.scoresaddr
printfNew player joined: selfclientsaddraddr
self.broadcastmessagefWelcome selfclientsaddr Current players: joinselfclients.values
except socket.timeout:
pass
def broadcastmessageself message:
Send a message to all connected clients."""
for client in self.clients:
self.serversocket.sendtomessageencode client
and the rest of code can not added beacause space
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
