|
| 1 | +""" |
| 2 | +Simple Quiz Game |
| 3 | +--------------------------------- |
| 4 | +This program asks the user a series of multiple-choice questions. |
| 5 | +It keeps track of the score and displays the final result at the end. |
| 6 | + |
| 7 | +Concepts used: |
| 8 | +- Lists and dictionaries |
| 9 | +- Loops |
| 10 | +- Conditional logic |
| 11 | +- Basic input/output handling |
| 12 | +""" |
| 13 | + |
| 14 | +def run_quiz(questions): |
| 15 | + """ |
| 16 | + Runs the quiz loop and calculates the user's score. |
| 17 | + |
| 18 | + Parameters: |
| 19 | + questions (list): A list of question dictionaries, each containing: |
| 20 | + 'question', 'options', and 'answer' keys. |
| 21 | + """ |
| 22 | + score = 0 # To keep track of correct answers |
| 23 | + |
| 24 | + print("🧠 Welcome to the Python Quiz Game!") |
| 25 | + print("Try to answer all questions correctly.\n") |
| 26 | + |
| 27 | + # Loop through each question in the list |
| 28 | + for index, q in enumerate(questions, start=1): |
| 29 | + print(f"Q{index}: {q['question']}") |
| 30 | + |
| 31 | + # Display all options neatly |
| 32 | + for option in q['options']: |
| 33 | + print(option) |
| 34 | + |
| 35 | + # Get user's answer |
| 36 | + user_answer = input("Your answer (A/B/C/D): ").strip().upper() |
| 37 | + |
| 38 | + # Check if the answer is correct |
| 39 | + if user_answer == q['answer']: |
| 40 | + print("✅ Correct!\n") |
| 41 | + score += 1 |
| 42 | + else: |
| 43 | + print(f"❌ Wrong! The correct answer was {q['answer']}.\n") |
| 44 | + |
| 45 | + # After all questions, show the score |
| 46 | + print("📊 Quiz Completed!") |
| 47 | + print(f"Your final score: {score}/{len(questions)}") |
| 48 | + |
| 49 | + # Optional encouragement based on performance |
| 50 | + if score == len(questions): |
| 51 | + print("🏆 Excellent! You nailed it!") |
| 52 | + elif score >= len(questions) // 2: |
| 53 | + print("👍 Good job! Keep practicing.") |
| 54 | + else: |
| 55 | + print("💪 Don’t worry, you’ll get better next time!") |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + """Main entry point — defines the questions and starts the quiz.""" |
| 60 | + |
| 61 | + # List of question dictionaries |
| 62 | + questions = [ |
| 63 | + { |
| 64 | + "question": "What is the output of print(2 ** 3)?", |
| 65 | + "options": ["A) 6", "B) 8", "C) 9", "D) 12"], |
| 66 | + "answer": "B" |
| 67 | + }, |
| 68 | + { |
| 69 | + "question": "Which keyword is used to define a function in Python?", |
| 70 | + "options": ["A) func", "B) define", "C) def", "D) function"], |
| 71 | + "answer": "C" |
| 72 | + }, |
| 73 | + { |
| 74 | + "question": "What data type is used to store True or False?", |
| 75 | + "options": ["A) int", "B) bool", "C) str", "D) float"], |
| 76 | + "answer": "B" |
| 77 | + }, |
| 78 | + { |
| 79 | + "question": "Which of these is a valid variable name?", |
| 80 | + "options": ["A) 1name", "B) first-name", "C) first_name", "D) first name"], |
| 81 | + "answer": "C" |
| 82 | + }, |
| 83 | + ] |
| 84 | + |
| 85 | + # Start the quiz |
| 86 | + run_quiz(questions) |
| 87 | + |
| 88 | + |
| 89 | +# Run the main function only when executed directly |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments