Thursday, 6 April 2023

Quiz

# Quiz program # Questions and answers questions = [ { "question": "What is the capital of India?", "options": ["Mumbai", "New Delhi", "Chennai", "Kolkata"], "answer": "New Delhi" }, { "question": "What is the largest mammal in the world?", "options": ["Elephant", "Giraffe", "Blue Whale", "Hippopotamus"], "answer": "Blue Whale" }, { "question": "What is the currency of Japan?", "options": ["Yen", "Dollar", "Pound", "Euro"], "answer": "Yen" }, { "question": "Who is the CEO of Microsoft?", "options": ["Jeff Bezos", "Satya Nadella", "Mark Zuckerberg", "Tim Cook"], "answer": "Satya Nadella" }, { "question": "Which planet in our solar system is known as the 'Red Planet'?", "options": ["Venus", "Mars", "Jupiter", "Saturn"], "answer": "Mars" } ] # Function to display questions and get user's answers def ask_question(question, options): print(question) for i in range(len(options)): print(f"{i+1}. {options[i]}") answer = input("Enter your answer (1-4): ") return answer # Main function def main(): score = 0 print("Welcome to the Quiz!") for i in range(len(questions)): answer = ask_question(questions[i]["question"], questions[i]["options"]) if answer == "quit": break if answer == questions[i]["answer"]: print("Correct!") score += 1 else: print("Wrong!") print(f"Your final score is {score} out of {len(questions)}") # Call main function main()

No comments:

Post a Comment

Code 2