python在线考试系统
时间: 2024-01-13 17:20:18 浏览: 28
Python可以用来开发在线考试系统。以下是一个简单的示例:
```python
import random
class Exam:
def __init__(self, questions):
self.questions = questions
def start_exam(self):
score = 0
random.shuffle(self.questions)
for i, question in enumerate(self.questions):
print(f"Question {i+1}: {question['text']}")
for j, option in enumerate(question['options']):
print(f"{j+1}. {option}")
answer = input("Your answer: ")
if answer == question['answer']:
score += 1
print(f"Your score: {score}/{len(self.questions)}")
# Example usage
questions = [
{
'text': 'What is the capital of France?',
'options': ['London', 'Paris', 'Berlin', 'Madrid'],
'answer': '2'
},
{
'text': 'What is the largest planet in our solar system?',
'options': ['Mars', 'Jupiter', 'Earth', 'Saturn'],
'answer': '2'
},
{
'text': 'Who painted the Mona Lisa?',
'options': ['Leonardo da Vinci', 'Pablo Picasso', 'Vincent van Gogh', 'Michelangelo'],
'answer': '1'
}
]
exam = Exam(questions)
exam.start_exam()
```
这个示例创建了一个Exam类,它接受一个问题列表作为参数。start_exam方法随机打乱问题的顺序,并逐个显示问题和选项,然后要求用户输入答案。最后,它会计算并显示用户的得分。