ValueError:没有足够的值可解包(预期为6,得到5)

时间:2018-11-19 00:10:41

标签: python

我试图只用角色制作一个python游戏,说实话我做到了,但是最终条件太模糊了。首先,我只是将startLocation分配为正常位置,完成目标之后,我将返回起始位置并退出游戏以给出新的行文本。但是:

def locations():

  startLocation = random.choice(mapaGrid)
  monsterLocation = random.choice(mapaGrid)
  wellLocation = random.choice(mapaGrid)
  goldLocation = random.choice(mapaGrid)
  arrowLocation = random.choice(mapaGrid)

  if monsterLocation == goldLocation or monsterLocation == startLocation or goldLocation == startLocation or monsterLocation == arrowLocation or wellLocation == goldLocation or wellLocation == startLocation or wellLocation == monsterLocation:
    return locations()

  return startLocation, monsterLocation, goldLocation, arrowLocation, wellLocation

#Locais do jogador, monstro, ouro, poco, flecha e entrada.
playerLocation, monsterLocation, goldLocation, arrowLocation, wellLocation, startLocation = locations()

所以这就是代码失败的地方。最后,当我将startLocation分配给locations()时,即使添加任何其他完全组成的位置也不会导致此错误,但标题中会出现错误。我确实尝试搜索,但是由于经验不足,我无法将答案与我的代码关联。

1 个答案:

答案 0 :(得分:1)

您返回5个元素:

return startLocation, monsterLocation, goldLocation, arrowLocation, wellLocation
       ^1             ^2               ^3            ^4             ^5

在尝试从函数中设置的值中-有六个:

playerLocation, monsterLocation, goldLocation, arrowLocation, wellLocation, startLocation = locations()
^1              ^2               ^3            ^4             ^5            ^6

您希望python放入第6个变量吗?
您得到的错误是您不能分配给6个变量,因为您只返回了5个。