创建一个选择题类ChoiceQuestion,包含成员变量:题干(字符串)、可选答案(字符串数组)、正确答案(字符串),包含构造函数(带一个字符串参数,可以对字符串参数解析,并根据解析结果对成员变量赋值)和成员函数:输出题目,判断答案是否正确。输出题目的格式如下: He cut cloth with__sissors. A. a couple of B. a pair of C. two D. a
时间: 2024-03-16 20:47:41 浏览: 16
好的,我理解了你的要求。以下是ChoiceQuestion类的实现代码:
```python
class ChoiceQuestion:
def __init__(self, question_str):
self.question_str = question_str
self.options = []
self.answer = ""
self.parse_question_str(question_str)
def parse_question_str(self, question_str):
# 解析题目字符串,分离题干、选项和正确答案
parts = question_str.split(".")
self.question_str = parts[0]
options_str = parts[1].strip()
self.options = options_str.split(" ")
self.answer = self.options[0]
def print_question(self):
# 输出题目
print(self.question_str + ".")
for i in range(len(self.options)):
print(chr(ord('A') + i) + ".", self.options[i])
def check_answer(self, user_answer):
# 判断答案是否正确
return user_answer.upper() == self.answer.upper()
```
这个类的构造函数接收一个字符串参数,解析字符串并初始化成员变量。parse_question_str()方法负责解析字符串,将题干、选项和正确答案分离开来。print_question()方法输出题目,格式与你给出的例子相同。check_answer()方法用于检查用户的答案是否正确。注意这个方法中的upper()方法可以将字符串转换为大写,以避免大小写敏感的问题。