|
| 1 | +def display_question(question, options): |
| 2 | + print(question) |
| 3 | + for index, option in enumerate(options, start=1): |
| 4 | + print(f"{index}. {option}") |
| 5 | + |
| 6 | +def get_user_answer(): |
| 7 | + while True: |
| 8 | + try: |
| 9 | + answer = int(input("Enter the number of your answer: ")) |
| 10 | + return answer |
| 11 | + except ValueError: |
| 12 | + print("Invalid input. Please enter a number.") |
| 13 | + |
| 14 | +def run_quiz(questions): |
| 15 | + score = 0 |
| 16 | + for question, data in questions.items(): |
| 17 | + display_question(question, data['options']) |
| 18 | + user_answer = get_user_answer() |
| 19 | + correct_answer = data['answer'] |
| 20 | + |
| 21 | + if user_answer == correct_answer: |
| 22 | + print("Correct!") |
| 23 | + score += 1 |
| 24 | + else: |
| 25 | + print(f"Incorrect. The correct answer was {correct_answer}.") |
| 26 | + print() |
| 27 | + |
| 28 | + print(f"Quiz complete! Your final score is {score} out of {len(questions)}.") |
| 29 | + |
| 30 | +def main(): |
| 31 | + questions = { |
| 32 | + "Who is the Current President Of America?": { |
| 33 | + "options": ["Obama", "Biden", "Elon", "Putin"], |
| 34 | + "answer": 2 |
| 35 | + }, |
| 36 | + "What is 2 + 2?": { |
| 37 | + "options": ["3", "4", "5", "6"], |
| 38 | + "answer": 2 |
| 39 | + }, |
| 40 | + "Which planet is known as the Red Planet?": { |
| 41 | + "options": ["Earth", "Mars", "Jupiter", "Saturn"], |
| 42 | + "answer": 2 |
| 43 | + }, |
| 44 | + "Who is the Creator of Python?": { |
| 45 | + "options": ["Guido van Rossum", "James Gosling", "Brendan Eich", "Mark Zukerberg"], |
| 46 | + "answer": 1 |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + run_quiz(questions) |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments