Python 考试系统代码
时间: 2024-03-21 22:35:59 浏览: 127
Python考试系统代码是一个用Python编写的程序,用于实现在线考试功能。它可以提供题目的展示、答题、自动评分等功能。以下是一个简单的Python考试系统代码示例:
```python
class Question:
def __init__(self, question, choices, answer):
self.question = question
self.choices = choices
self.answer = answer
def display_question(self):
print(self.question)
for i, choice in enumerate(self.choices):
print(f"{i+1}. {choice}")
def check_answer(self, user_answer):
return user_answer == self.answer
class Exam:
def __init__(self, questions):
self.questions = questions
self.score = 0
def take_exam(self):
for question in self.questions:
question.display_question()
user_answer = input("Your answer: ")
if question.check_answer(user_answer):
self.score += 1
def get_result(self):
print(f"Your score: {self.score}/{len(self.questions)}")
# 创建题目
question1 = Question("What is the capital of France?", ["A. Paris", "B. London", "C. Rome"], "A")
question2 = Question("What is the largest planet in our solar system?", ["A. Jupiter", "B. Earth", "C. Mars"], "A")
question3 = Question("What is the symbol for hydrogen?", ["A. H", "B. O", "C. He"], "A")
# 创建考试
exam = Exam([question1, question2, question3])
# 开始考试
exam.take_exam()
# 获取成绩
exam.get_result()
```
阅读全文