python编写选择题程序
时间: 2023-08-10 16:03:57 浏览: 105
以下是一个简单的Python选择题程序的示例代码:
```python
questions = [
{
"question": "What is the capital of France?",
"options": ["London", "Paris", "Berlin", "Madrid"],
"answer": "Paris"
},
{
"question": "What is the largest planet in our solar system?",
"options": ["Jupiter", "Saturn", "Neptune", "Mercury"],
"answer": "Jupiter"
},
{
"question": "What is the smallest country in the world?",
"options": ["Monaco", "Vatican City", "San Marino", "Liechtenstein"],
"answer": "Vatican City"
}
]
score = 0
for q in questions:
print(q["question"])
for i, option in enumerate(q["options"]):
print(f"{i+1}. {option}")
user_answer = input("Your answer: ")
if user_answer.lower() == q["answer"].lower():
print("Correct!")
score += 1
else:
print("Incorrect!")
print(f"Final score: {score}/{len(questions)}")
```
在上面的代码中,我们首先定义了一个包含三个问题的列表,每个问题都包含问题、选项和答案。然后,我们使用一个for循环遍历每个问题,并打印问题和选项。用户可以输入他们的答案,然后我们检查它是否与正确的答案匹配。最后,我们打印出用户的最终得分。
请注意,上面的代码只是一个简单的示例,可以根据需要进行修改或扩展。
阅读全文