Me gustaría que en la de la función tiempoagotado, cuando aparezca se agotó el tiempo,…
- User
Me gustaría que en la de la función tiempo_agotado, cuando aparezca se agotó el tiempo, no me repita la pregunta, sino que pase a la opción de deseas jugar otra vez: import json import random import threading
puntaje = 0 def cargar_preguntas(archivo): with open(archivo, "r", encoding="utf-8") as f: preguntas = json.load(f) return preguntas
def tiempo_agotado(): print("\nSe agotó el tiempo. Fin del juego.") print(f"Tu puntaje es: {puntaje}")
def temporizador(tiempo): for i in range(tiempo, 0, -1): if t.status: print(f"\r{i} segundos restantes", end="") t.sleep(1) else: threading.currentThread().cancel()
def jugar(preguntas): random.shuffle(preguntas) puntaje = 0
t = threading.currentThread() for pregunta_obj in preguntas: t.status = True timer = threading.Timer(10, tiempo_agotado) timer.start() pregunta = pregunta_obj["pregunta"] opciones = pregunta_obj["opciones"] respuesta_correcta = pregunta_obj["respuesta_correcta"] print(f"\nPregunta: {pregunta}") for i, opcion in enumerate(opciones, start=1): print(f"{i}. {opcion}") respuesta_usuario = None while respuesta_usuario not in ["1", "2", "3", "4", "s"]: respuesta_usuario = input("Seleccione el número de la opción correcta (o 's' para salir): ") if respuesta_usuario == 's': print(f"¡Gracias por jugar! Has salido del juego. Tu puntaje actual es: {puntaje}") t.status = False timer.cancel() return puntaje if respuesta_usuario not in ["1", "2", "3", "4"]: print("Opción inválida. Por favor, ingrese un número válido o 's' para salir.") respuesta_usuario = int(respuesta_usuario) t.status = False timer.cancel() if opciones[respuesta_usuario - 1] == respuesta_correcta: print("¡Correcto!") puntaje += 1 else: print(f"Incorrecto. La respuesta correcta es: {respuesta_correcta}") break print(f"\nTu puntaje es: {puntaje}") return puntajedef jugar_de_nuevo(): respuesta = input("¿Deseas jugar de nuevo? (s/n): ") return respuesta.lower() == "s"
preguntas = cargar_preguntas("preguntas.json")
jugando = True while jugando: t = threading.Thread(target=temporizador, args=(10,)) puntaje = jugar(preguntas) jugando = jugar_de_nuevo()
print("Gracias por jugar. ¡Hasta luego!")
- Assistant