命令行游戏引擎报错:TypeError: 'bool' object is not iterable 求助
问题分析与解决
错误原因
第233行的语法逻辑完全错误:active_quests not in completed_quests是判断整个active_quests列表是否存在于completed_quests中,这个表达式返回的是布尔值(True/False)。而for循环需要遍历可迭代对象(比如列表),遍历布尔值就会触发TypeError: 'bool' object is not iterable错误。
你的需求应该是遍历active_quests中不在completed_quests里的任务,而非判断整个列表是否在另一个列表中。
修正代码
把第233行改成筛选active_quests中符合条件的元素,同时自动处理空列表的情况:
232 #quest handler 233 for quest in (q for q in active_quests if q not in completed_quests): 234 quest_type = data.quests[quest]["type"] 235 if quest_type == "kill": 236 #check if thing has been killed 237 if data.quests[quest]["number"] in fights_won: 238 unrewarded_quests.append(quest)
效果说明
- 当
active_quests为空时,生成器表达式(q for q in active_quests if q not in completed_quests)没有任何元素,for循环直接跳过,不会执行循环体代码,自然不会崩溃。 - 当
active_quests有元素时,只会遍历那些不在completed_quests里的任务,符合业务逻辑。
内容的提问来源于stack exchange,提问作者mr.hooman
相关产品推荐
相关产品推荐

