class Quiz: def __init__(self, questions): self.questions = questions self.score = 0 def ask_question(self, question, options, correct_option): print(question) for i, option in enumerate(options, 1): print(f"{i}. {option}") while True: try: choice = int(input("Enter the number of your choice: ")) if 1 <= choice <= len(options): break else: print("Invalid choice. Please enter a valid number.") except ValueError: print("Invalid input. Please enter a number.") if options[choice - 1] == correct_option: print("Correct!\n") self.score += 1 else: print("Incorrect. The correct answer is:", correct_option, "\n") def start(self): print("Welcome to the Quiz Game!\n") for i, (question, options, correct_option) in enumerate(self.questions, 1): print("Question", i) self.ask_question(question, options, correct_option) print("Quiz complete! Your score:", self.score, "out of", len(self.questions)) # List of questions with options and correct answers questions = [ ("What is the capital of India?", ["Uttarpradesh", "Gujrat", "Punjab", "New Delhi"], "New Delhi"), ("Which planet is known as the 'Red Planet'?", ["Earth", "Mars", "Jupiter", "Venus"], "Mars"), ("What is the largest mammal?", ["Blue Whale", "Elephant", "Giraffe", "Lion"], "Blue Whale"), ("World wide web is being standard by?", ["w3c", "worldwide coperation", "world wide web standard", "None of these"], "w3c"), ("Who is the father of C language?", ["Bjarne stroustrup", "James A.Gosling", "Dennis Ritchie", "Dr. E.F. Codd"], "Dennis Ritchie"), ("C Program are converting into machine language with help of ?", ["An editor", "Compiler", "an operating system", "None of these"], "Compiler"), ] # Create a Quiz instance and start the game quiz = Quiz(questions) quiz.start() OUTPUT : Welcome to the Quiz Game! Question 1 What is the capital of India? 1. Uttarpradesh 2. Gujrat 3. Punjab 4. New Delhi Enter the number of your choice: 4 Correct! Question 2 Which planet is known as the 'Red Planet'? 1. Earth 2. Mars 3. Jupiter 4. Venus Enter the number of your choice: 2 Correct! Question 3 What is the largest mammal? 1. Blue Whale 2. Elephant 3. Giraffe 4. Lion Enter the number of your choice: 1 Correct! Question 4 World wide web is being standard by? 1. w3c 2. worldwide coperation 3. world wide web standard 4. None of these Enter the number of your choice: 1 Correct! Question 5 Who is the father of C language? 1. Bjarne stroustrup 2. James A.Gosling 3. Dennis Ritchie 4. Dr. E.F. Codd Enter the number of your choice: 3 Correct! Question 6 C Program are converting into machine language with help of ? 1. An editor 2. Compiler 3. an operating system 4. None of these Enter the number of your choice: 2 Correct! Quiz complete! Your score: 6 out of 6
Quiz Game with Options using Python
Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated