PROJECT 1: GEOGRAPHY QUIZ
import json
import random
with open("C:/Users/brian/Desktop/portfolio/portfolio/python/intermediate/Quiz/questions.json") as f:
questions = json.load(f)
play = input("Do you want to play a geography quiz?")
def ask_questions(level_questions, number_of_questions):
score = 0
random.shuffle(level_questions)
for i, q in enumerate(level_questions[:number_of_questions]):
print(f"Question {i + 1}: {q['question']}")
for j, option in enumerate(q["options"]):
print(f"Option {j + 1}: {option}")
try:
answer = int(input("Answer: (1 to 4): "))
if 1 <= answer <= 4:
if q["options"][answer - 1] == q["answer"]:
print("correct")
score += 1
else:
print("Wrong! Next question")
else:
print("Please enter a valid option between from 1 to 4")
except ValueError:
print("Please enter a valid option between from 1 to 4")
return score
def play_quiz():
number_of_questions = 5
level = input("What level of difficulty do you want to play at? (easy, medium, hard): ")
if level in questions:
score = ask_questions(questions[level], number_of_questions)
print(f"Your score is {score}/{number_of_questions}.")
else:
print("Invalid difficulty level. Please choose from easy, medium, or hard.")
if play == "yes" or play == "Y":
print("Welcome to the geography quiz!")
play_quiz()
else:
print("Goodbye!")
This project is a command-line-based geography quiz game that allows users to test their knowledge by answering random questions. The quiz features multiple-choice questions that are loaded from a JSON file, with options for selecting different difficulty levels: easy, medium, or hard.
Key Features:
Difficulty Levels: Players can choose between three difficulty levels, each with a set of questions.
Randomized Questions: The questions are shuffled randomly for each playthrough to provide a unique experience.
Multiple-Choice Options: Each question comes with four possible answers, and players must select the correct one by entering a number (1 to 4).
Score Tracking: The game tracks the player’s score and provides feedback on correct and incorrect answers.
Error Handling: The program gracefully handles invalid input, ensuring that players enter valid answers.
This project demonstrates intermediate Python skills such as file handling (loading questions from a JSON file), loops, conditional statements, and basic error handling.