Quiz

share link

by vipulnp123 dot icon Updated: Oct 26, 2023

technology logo
technology logo

Guide Kit Guide Kit  

class Question:

def __init__(self, prompt, options, correct_option):

self.prompt = prompt

self.options = options

self.correct_option = correct_option


def ask_question(self):

print(self.prompt)

for i, option in enumerate(self.options, 1):

print(f"{i}. {option}")

user_answer = input("Enter the number of your answer: ")

return int(user_answer) == self.correct_option



def run_quiz(questions):

score = 0

for question in questions:

if question.ask_question():

print("Correct!\n")

score += 1

else:

print("Incorrect. The correct answer was option", question.correct_option, "\n")


print("You got", score, "out of", len(questions), "questions correct.")



# Define your questions here

question1 = Question("What is the capital of France?", ["Paris", "Rome", "Berlin", "Madrid"], 1)

question2 = Question("What is the largest planet in our solar system?", ["Jupiter", "Mars", "Venus", "Saturn"], 1)

question3 = Question("What is 2 + 2?", ["3", "4", "5", "6"], 2)


# Add more questions if desired


questions = [question1, question2, question3]


# Run the quiz

run_quiz(questions)


Deployment Information

DHRUV PRAJAPTI

See similar Kits and Libraries