Question
Write a short quiz program which asks two true/false questions and stores the user's answers as booleans. At the end of the program it should print the user's answers with the correct answers.
Answers
Answered by
GPT-4o mini
Below is a short quiz program written in Python that asks two true/false questions, stores the user's answers as booleans, and then prints the user's answers along with the correct answers.
```python
def main():
# Initialize questions and answers
questions = [
("The Earth is flat.", False), # Question 1
("Python is a programming language.", True) # Question 2
]
user_answers = [] # To store user's boolean answers
# Ask each question
for question, correct_answer in questions:
# Input from user and convert to boolean
answer = input(f"True or False: {question} ").strip().lower()
if answer == 'true':
user_answers.append(True)
elif answer == 'false':
user_answers.append(False)
else:
print("Invalid input! Please answer with 'True' or 'False'.")
return # Exit if invalid input
# Print results
print("\nYour Answers:")
for i in range(len(questions)):
print(f"Question {i + 1}: {questions[i][0]}")
print(f"Your answer: {user_answers[i]} (Correct answer: {questions[i][1]})\n")
if __name__ == "__main__":
main()
```
### How the Program Works:
1. **Questions and Answers**: The program defines a list of tuples, where each tuple contains a question and its corresponding correct answer (boolean).
2. **User Input**: It loops through each question, asking for the user's input. The input is expected to be either "True" or "False". It converts the input to a boolean and stores it in the `user_answers` list.
3. **Invalid Input Handling**: If the input is neither "True" nor "False", it informs the user and exits the program.
4. **Results Display**: After all questions are answered, the program prints each question, the user's answer, and the correct answer.
### To Run the Program:
Make sure you have Python installed, create a `.py` file with the above code, and then run it using a command line or terminal.
```python
def main():
# Initialize questions and answers
questions = [
("The Earth is flat.", False), # Question 1
("Python is a programming language.", True) # Question 2
]
user_answers = [] # To store user's boolean answers
# Ask each question
for question, correct_answer in questions:
# Input from user and convert to boolean
answer = input(f"True or False: {question} ").strip().lower()
if answer == 'true':
user_answers.append(True)
elif answer == 'false':
user_answers.append(False)
else:
print("Invalid input! Please answer with 'True' or 'False'.")
return # Exit if invalid input
# Print results
print("\nYour Answers:")
for i in range(len(questions)):
print(f"Question {i + 1}: {questions[i][0]}")
print(f"Your answer: {user_answers[i]} (Correct answer: {questions[i][1]})\n")
if __name__ == "__main__":
main()
```
### How the Program Works:
1. **Questions and Answers**: The program defines a list of tuples, where each tuple contains a question and its corresponding correct answer (boolean).
2. **User Input**: It loops through each question, asking for the user's input. The input is expected to be either "True" or "False". It converts the input to a boolean and stores it in the `user_answers` list.
3. **Invalid Input Handling**: If the input is neither "True" nor "False", it informs the user and exits the program.
4. **Results Display**: After all questions are answered, the program prints each question, the user's answer, and the correct answer.
### To Run the Program:
Make sure you have Python installed, create a `.py` file with the above code, and then run it using a command line or terminal.