No title

2 minute read
0
class MCQ: def __init__(self, question, options, correct_answer): self.question = question self.options = options self.correct_answer = correct_answer def display(self): print(f"Q: {self.question}") for i, option in enumerate(self.options): print(f"{i+1}. {option}") answer = int(input("Your answer (1-4): ")) return answer == self.correct_answer def display_result(score, total): print("\nQuiz Completed!") print(f"Your Score: {score}/{total}") percentage = (score / total) * 100 print(f"Percentage: {percentage}%") def main(): # List of questions, options, and correct answer indices questions = [ MCQ("What is the capital of India?", ["Mumbai", "Delhi", "Kolkata", "Chennai"], 2), MCQ("Who wrote the Indian National Anthem?", ["Rabindranath Tagore", "Bankim Chandra Chatterjee", "Sarojini Naidu", "Mahatma Gandhi"], 1), MCQ("Which planet is known as the Red Planet?", ["Earth", "Mars", "Jupiter", "Venus"], 2), MCQ("What is the largest mammal?", ["Elephant", "Blue Whale", "Giraffe", "Shark"], 2) ] score = 0 total = len(questions) # Loop through each question and collect answers for question in questions: if question.display(): score += 1 # Display the final result display_result(score, total) # Run the quiz if __name__ == "__main__": main()

Post a Comment

0Comments
Post a Comment (0)