如何用Python面向对象编程实现性格测试问卷?已有初步尝试但计算逻辑存疑求帮助
面向对象实现性格测试问卷的完整解决方案
看起来你已经有了不错的基础思路,不过在类的初始化、方法逻辑和类之间的协作上还有些小问题。我来帮你把这些部分整合起来,完成一个完整的面向对象版本:
第一步:重构QBank类
你的QBank类需要正确封装问题数据、维护各维度的百分比,并且实现提问逻辑。注意修正__init__的拼写(你写成_init_了,少了一个下划线),同时把问题和对应的维度关联起来,而不是在方法里重新定义。
第二步:完善PType类
PType类需要接收QBank计算出的百分比数据,实现性格类型的推导逻辑,同时可以扩展你想要的推荐内容(动漫角色、职业等)。
完整实现代码
class QBank: def __init__(self): # 初始化问题列表,每个问题包含题目和对应的维度 self.questions = [ {"q": "Would you call yourself a thinker", "dimension": "N"}, {"q": "Are you creative", "dimension": "N"}, {"q": "Do you like philosophy", "dimension": "N"}, {"q": "Do you like to think about complex questions", "dimension": "N"}, {"q": "Do you have an interest in artistic pursuits like painting and writing", "dimension": "N"}, {"q": "Do you like initiating conversations", "dimension": "E"}, {"q": "Do you like to start talks with new people", "dimension": "E"}, {"q": "Is it easy for you to adjust in an environment where you initially do not know anyone", "dimension": "E"}, {"q": "Are you social", "dimension": "E"}, {"q": "Do you like social events (parties, fests and events)", "dimension": "E"}, {"q": "Do you mostly make your decisions by heart", "dimension": "F"}, {"q": "Do you put others before yourself", "dimension": "F"}, {"q": "Do you think that one should change their views if they hurt someone", "dimension": "F"}, {"q": "Do you often get lost in your feelings", "dimension": "F"}, {"q": "Are you run more by your feelings than logic", "dimension": "F"}, {"q": "Can you make a timetable and stick to it", "dimension": "J"}, {"q": "Do you usually follow the rules/laws", "dimension": "J"}, {"q": "Are you organised", "dimension": "J"}, {"q": "Do you plan before doing something", "dimension": "J"}, {"q": "Are you ok with following orders", "dimension": "J"} ] # 初始化各维度的百分比,每个维度5道题,每题20分 self.dimension_percent = { "E": 0, # 外向/内向 "N": 0, # 直觉/感觉 "F": 0, # 情感/思考 "J": 0 # 判断/感知 } def conduct_test(self): """执行测试,遍历问题并收集回答,计算百分比""" try: for idx, q_data in enumerate(self.questions, 1): # 获取用户回答,转换为布尔值(Yes/No) response = input(f"{idx}. {q_data['q']}? Yes/No:\t").strip().lower() if response in ["yes", "y"]: # 答对(选择符合维度的选项)则增加对应百分比 self.dimension_percent[q_data['dimension']] += 20 except KeyboardInterrupt: print("\n测试被中断,退出程序") exit(0) def get_percentages(self): """返回各维度的百分比数据""" return self.dimension_percent class PType: def __init__(self, percent_data): self.percent_data = percent_data self.personality_type = self._calculate_personality() # 可以扩展不同性格类型的对应内容 self.type_details = { "ENTJ": { "anime": "Mikasa Ackerman", "movie": "Harrison Wells", "career": ["Business Executive", "Lawyer", "Project Manager"] }, "INFP": { "anime": "Levi Ackerman", "movie": "Elsa (Frozen)", "career": ["Writer", "Counselor", "Artist"] }, # 可以继续添加其他类型的内容 } def _calculate_personality(self): """根据百分比推导性格类型""" # 外向(E)/内向(I):E>50则为E,否则I ei = "E" if self.percent_data["E"] > 50 else "I" # 直觉(N)/感觉(S):N>50则为N,否则S ns = "N" if self.percent_data["N"] > 50 else "S" # 情感(F)/思考(T):F>50则为F,否则T ft = "F" if self.percent_data["F"] > 50 else "T" # 判断(J)/感知(P):J>50则为J,否则P jp = "J" if self.percent_data["J"] > 50 else "P" return ei + ns + ft + jp def print_results(self): """打印完整的测试结果""" print("\n--- 性格维度百分比 ---") print(f"外向性(E)百分比: {self.percent_data['E']}%") print(f"直觉性(N)百分比: {self.percent_data['N']}%") print(f"情感性(F)百分比: {self.percent_data['F']}%") print(f"判断性(J)百分比: {self.percent_data['J']}%") print("\n--- 你的性格类型 ---") print(f"性格类型: {self.personality_type}") # 输出对应推荐内容(如果存在) if self.personality_type in self.type_details: details = self.type_details[self.personality_type] print(f"对应动漫角色: {details['anime']}") print(f"对应影视角色: {details['movie']}") print("推荐职业方向: " + ", ".join(details['career'])) else: print("暂未收录该性格类型的详细推荐内容") # 运行测试流程 if __name__ == "__main__": # 创建题库实例 q_bank = QBank() # 执行测试 q_bank.conduct_test() # 获取百分比数据 percentages = q_bank.get_percentages() # 创建性格类型实例并打印结果 personality = PType(percentages) personality.print_results()
关键改进点说明
QBank类:
- 修正了
__init__的拼写错误,确保类能正确初始化 - 将问题数据封装在类内部,通过
conduct_test方法完成提问和百分比计算 - 提供
get_percentages方法让其他类可以获取计算结果
- 修正了
PType类:
- 通过构造函数接收QBank的百分比数据,实现类之间的协作
- 用私有方法
_calculate_personality完成性格类型的推导逻辑 - 扩展了类型对应的推荐内容,让结果更丰富
整体流程:
分离了题库管理/测试执行和性格类型计算/结果展示的职责,符合面向对象的单一职责原则,代码更易维护和扩展。
内容的提问来源于stack exchange,提问作者merciivi
相关产品推荐
相关产品推荐

