Add to the code a timer that starts at 10 seconds and reaches 0 for each question,…
- User
Add to the code a timer that starts at 10 seconds and reaches 0 for each question, showing on the terminal as the time decreases, if the user answers correctly, the timer is reset and the same process is repeated, if the time runs out, it will send a game over message: import json import random
def cargar_preguntas(archivo): with open(archivo, "r", encoding="utf-8") as f: preguntas = json.load(f) return preguntas
def jugar(preguntas): random.shuffle(preguntas) puntaje = 0
codefor pregunta_obj in preguntas: 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}") 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) 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 puntajeFunción para preguntar al usuario si desea jugar nuevamente
def jugar_de_nuevo(): respuesta = input("¿Deseas jugar de nuevo? (s/n): ") return respuesta.lower() == "s"
preguntas = cargar_preguntas("preguntas.json")
- Assistant