CREATING A QUIZ_GAME USING PYTHON LANGUAGE
*step 1: Set Up Your Project
1.Create a new folder for your project and give it a suitable name, like "QuizGame".
2,Inside the project folder, create a Python file named quiz_game.py.
*Step 2: Design Your Quiz
Define your quiz questions and answers. You could use a list of dictionaries, where each dictionary represents a question with its options and correct answer.
python
Copy code
quiz_questions = [
{
'question': 'What is the capital of France?',
'options': ['London', 'Berlin', 'Paris', 'Madrid'],
'answer': 'Paris'
},
# Add more questions in a similar format
]
*Step 3: Create the Game Logic
Start by importing the necessary modules:
python
Copy code
import random
Define a function to shuffle the questions:
python
Copy code
def shuffle_questions(questions):
random.shuffle(questions)
return questions
Create a function to present the quiz questions to the user and check their answers:
python
Copy code
def play_quiz(questions):
score = 0
for question in questions:
print(question['question'])
for i, option in enumerate(question['options']):
print(f"{i + 1}. {option}")
user_choice = input("Enter the number of your answer: ")
if question['options'][int(user_choice) - 1] == question['answer']:
print("Correct!\n")
score += 1
else:
print(f"Wrong! The correct answer was: {question['answer']}\n")
print(f"You got {score} out of {len(questions)} questions correct!")
# Shuffle the questions and start the quiz
shuffled_questions = shuffle_questions(quiz_questions)
play_quiz(shuffled_questions)
Step 4: Run the Quiz
At the end of your quiz_game.py file, add the following code to run the quiz:
python
Copy code
if __name__ == "__main__":
shuffled_questions = shuffle_questions(quiz_questions)
play_quiz(shuffled_questions)
Step 5: Run Your Quiz
Open your terminal or command prompt.
Navigate to the project folder using the cd command.
Run the quiz game by executing the command: python quiz_game.py.
Congratulations! You've created a simple quiz game in Python. Players will be presented with questions, can choose options, and get a score at the end. You can expand this game by adding more features, like a timer, different difficulty levels, or saving high scores.
SOURCE CODE:
```python
import random
# Quiz questions and answers
quiz_questions = [
{
'question': 'What is the capital of France?',
'options': ['London', 'Berlin', 'Paris', 'Madrid'],
'answer': 'Paris'
},
{
'question': 'Which planet is known as the Red Planet?',
'options': ['Earth', 'Mars', 'Jupiter', 'Venus'],
'answer': 'Mars'
},
{
'question': 'What is the largest mammal?',
'options': ['Elephant', 'Giraffe', 'Whale', 'Rhino'],
'answer': 'Whale'
}
]
def shuffle_questions(questions):
random.shuffle(questions)
return questions
def play_quiz(questions):
score = 0
for question in questions:
print(question['question'])
for i, option in enumerate(question['options']):
print(f"{i + 1}. {option}")
user_choice = input("Enter the number of your answer: ")
if question['options'][int(user_choice) - 1] == question['answer']:
print("Correct!\n")
score += 1
else:
print(f"Wrong! The correct answer was: {question['answer']}\n")
print(f"You got {score} out of {len(questions)} questions correct!")
if __name__ == "__main__":
shuffled_questions = shuffle_questions(quiz_questions)
play_quiz(shuffled_questions)
```
You can copy and paste this code into a file named `quiz_game.py` and run it using the command `python quiz_game.py` in your terminal or command prompt. This code defines a simple quiz game where players are presented with questions, can choose options, and receive a score at the end. Feel free to modify and expand the code as you like!