import json def createquestion(question, options, answer): """ Creates a dictionary with…
- User
import json
def create_question(question, options, answer): """ Creates a dictionary with the question, options, and answer. """ return {'question': question, 'options': options, 'answer': answer}
def display_question(question): """ Displays the question and options to the user. """ print(question['question']) print("Options:") for index, option in enumerate(question['options']): print(f"{index + 1}. {option}") #print(f"{option}"
def get_user_answer(): """ Prompts the user to enter their answer and returns it. """ return int(input("Enter your answer (1-4): "))
def check_answer(question, user_answer): """ Checks if the user's answer matches the correct answer for the question. """ return user_answer == question['answer']
with open("./questions.json") as file: questions = json.load(file)
#questions = [
create_question("What is the capital of France?", ["1. London", "2. Paris", "3. Berlin", "4. Madrid"], 2),
create_question("Which planet is known as the Red Planet?", ["1. Venus", "2. Earth", "3. Mars", "4. Jupiter"], 3),
create_question("What is the largest organ in the human body?", ["1. Skin", "2. Heart", "3. Liver", "4. Lung"], 1)
#]
score = 0
Show and answer each question
for question in questions: display_question(question) user_answer = get_user_answer() if check_answer(question, user_answer): print("Correct!") score += 1 else: print("Incorrect!")
Display final score
print(f"Your score: {score}/{len(questions)}")
Make for it graphichal interface
- Assistant