如何修复Python 'Question' object is not subscriptable类型错误
True/False测验程序报错排查方案
错误根因
你遇到的TypeError: 'Question' object is not subscriptable由代码中几处核心问题直接导致:
- 基础语法缺失:存储题目的
question_data字典列表末尾没有写闭合的],会直接导致代码结构解析异常。 - 列表逻辑错误:你写的
question_bank = (question_data)没有创建新的空列表,只是把原始存字典的question_data列表直接赋值给了question_bank,二者指向内存里同一个列表。紧接着你在遍历question_data的同时,不断往同个列表里追加Question类实例,for循环按索引遍历到超出原始题目长度的位置时,取到的元素就不再是字典,而是你刚追加的Question对象。字典支持["key"]形式的下标取值,但自定义类实例默认不支持这种下标访问(也就是报错信息里的not subscriptable),这就是报错的直接触发原因。 - 方法调用错误:最后一行写的
quiz.next_question只是获取了方法对象,没有加括号(),根本不会执行方法内部的出题逻辑,就算修复了前面的问题,程序也不会正常运行。 - 功能逻辑缺失:现有
QuizBrain类没有答案校验、计分、遍历全部题目的逻辑,只能执行单次出题操作,无法完成完整测验流程。
修复后完整可运行代码
# 补全question_data的闭合括号 question_data = [ {"text": "A slug's blood is green.", "answer": "True"}, {"text": "The loudest animal is the African Elephant.", "answer": "False"}, {"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"}, {"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"}, {"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"}, {"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"}, {"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"}, {"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"}, {"text": "Google was originally called 'Backrub'.", "answer": "True"}, {"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"}, {"text": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"}, {"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"} ] class Question: def __init__(self, q_text, q_answer): self.text = q_text self.answer = q_answer class QuizBrain: def __init__(self, q_list): self.question_number = 0 self.question_list = q_list self.score = 0 # 判断是否还有未答题目 def still_has_questions(self): return self.question_number < len(self.question_list) def next_question(self): current_question = self.question_list[self.question_number] self.question_number += 1 user_answer = input(f"Q.{self.question_number}: {current_question.text} (True/False): ") self.check_answer(user_answer, current_question.answer) # 校验答案并更新分数 def check_answer(self, user_ans, correct_ans): if user_ans.lower() == correct_ans.lower(): self.score += 1 print("回答正确!") else: print("回答错误。") print(f"当前得分:{self.score}/{self.question_number}\n") # 初始化为空列表,不要直接绑定原始题目字典列表 question_bank = [] for question in question_data: question_text = question["text"] question_answer = question["answer"] new_question = Question(question_text, question_answer) question_bank.append(new_question) quiz = QuizBrain(question_bank) # 循环答完所有题目,调用方法必须加括号 while quiz.still_has_questions(): quiz.next_question() # 最终结算 print("答题结束!") print(f"你的最终得分是:{quiz.score}/{len(question_bank)}")
关键修复点说明
- 补全
question_data列表缺失的闭合方括号,解决基础语法错误 - 将
question_bank初始化为独立空列表,遍历原始题目字典时仅把转换后的Question实例追加到该空列表,从根源上避免遍历过程中混入类实例触发的下标取值错误 - 给方法调用补上括号,同时补充答案校验、计分、全题目遍历逻辑,让测验流程可以完整运行
- 答案判断时统一转小写,避免用户输入大小写和标准答案不匹配(比如输入
true而非True)被误判
内容的提问来源于stack exchange,提问作者Armand Surbakti
相关产品推荐
相关产品推荐

