from __future__ import annotations import json from pathlib import Path BASE_DIR = Path(__file__).resolve().parent QUIZ_PATH = BASE_DIR / "quiz.json" SCORE_PATH = BASE_DIR / "score.txt" def normalize(text: str) -> str: return text.strip().lower() def load_quiz(path: Path) -> list[dict]: # TODO: Wczytaj quiz z pliku JSON (json.load). # TODO: Sprawdź, czy wczytane dane są listą. # TODO: Usuń raise NotImplementedError po implementacji. with path.open("r", encoding="utf-8") as file: data = json.load(file) if not isinstance(data, list): raise ValueError("Quiz JSON musi być listą pytań") return data def run_quiz(questions: list[dict]) -> tuple[int, int]: # TODO: Zadaj pytania użytkownikowi w pętli i zlicz punkty. # TODO: Zwróć (score, total). # TODO: Usuń raise NotImplementedError po implementacji. score = 0 total = len(questions) for idx, item in enumerate(questions, start=1): question_text = str(item.get("q", "")) correct_answer = str(item.get("a", "")) print() print(f"Q{idx}/{total}: {question_text}") user_answer = input("Your answer: ") if normalize(user_answer) == normalize(correct_answer): print("Correct") score += 1 else: print("Wrong. Correct answer was: ", correct_answer) return score, total def save_score(path: Path, score: int, total: int) -> None: # TODO: Zapisz wynik do score.txt w formacie np. "3/3". # TODO: Usuń raise NotImplementedError po implementacji. with path.open("w", encoding="utf-8") as file: file.write(f"{score}/{total}\\n") def main() -> None: questions = load_quiz(QUIZ_PATH) score, total = run_quiz(questions) save_score(SCORE_PATH, score, total) print() print("Final score:", f"{score}/{total}") print("Score saved to:", SCORE_PATH.name) if __name__ == "__main__": main()