bool对象不可迭代



你好,我正在制作一个基于命令行的游戏引擎,当尝试在第233行上运行此代码时,不断弹出一个错误,它说"TypeError:‘bool’object is not iterable">

232  #quest handler
233  for quest in active_quests 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还是completed_quests是bool。据我所知,这两份名单都有。值如下

completed_quests = [0]
active_quests = []

我有一部分认为,因为acitve_quests中没有任何内容,所以它会返回为False我想知道如果活动任务中没有任何内容,如何使其不刹车。只要没有出什么问题,在已完成的任务中就应该有一些东西。完整的代码可以在replit和github上找到。github链接:https://github.com/mrhoomanwastaken/pypg/tree/people-and-quest-handler-overhaul

replit链接:https://replit.com/@mrhoomanwastake/pypg游戏引擎?v=1

您很接近,但语法有点偏离。您应该迭代一个满足条件的元素列表,但实际上您迭代的是模式a not in b的布尔值。

for quest in [q for q in active_quests if q not in completed_quests]:
...

active_quests not in completed_quests返回一个bool,这就是代码无法工作的原因。

for循环应该重写为类似的内容

for quest in [a for a in active_quests if a not in completed_quests]:

最新更新