【问题标题】:ValueError: not enough values to unpack (expected 6, got 5)ValueError:没有足够的值来解包(预期 6,得到 5)
【发布时间】:2019-04-21 08:21:14
【问题描述】:

我试图制作一个只使用角色的 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() 时,我在标题中得到错误,即使添加任何其他完全组成的位置不会导致此错误。我确实尝试过搜索,但由于我缺乏经验,我无法将答案与我的代码联系起来。

【问题讨论】:

  • Location 返回一个包含 5 个值的元组,您正试图将其分解为 6 ​​个值。 playerLocationstartLocation 有什么区别?
  • playerLocation 是玩家在网格上的直接位置,因此每次移动时都会更新它。 startLocation 是玩家开始游戏的随机网格选择。

标签: python


【解决方案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 个。

【讨论】:

  • 嗯,理解它的要点使我创建了第六个元素,称为空并将其分配为“0”。我希望保存 startLocation,但我不希望 playerLocation 被“返回”,因此期望第 6 个变量被“忽略”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2016-04-10
  • 2016-07-05
  • 2017-09-14
  • 2022-01-21
  • 2020-02-20
  • 1970-01-01
相关资源
最近更新 更多