Python ValueError:没有足够的值来解包(预期值为6,实际值为5)
我试图制作一个只使用角色的python游戏,老实说我做到了,但是最终条件太模糊了。为了制作一个,我简单地将startLocation指定为一个正常位置,完成目标后,我将返回起始位置并退出游戏以给出新的行文本。然而:Python ValueError:没有足够的值来解包(预期值为6,实际值为5),python,Python,我试图制作一个只使用角色的python游戏,老实说我做到了,但是最终条件太模糊了。为了制作一个,我简单地将startLocation指定为一个正常位置,完成目标后,我将返回起始位置并退出游戏以给出新的行文本。然而: def locations(): startLocation = random.choice(mapaGrid) monsterLocation = random.choice(mapaGrid) wellLocation = random.choice(mapaGri
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()时,我在标题中得到了错误,即使添加任何其他完全虚构的位置都不会导致此错误。我确实尝试过搜索,但由于缺乏经验,我无法将答案与我的代码联系起来 返回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个变量,因为您只返回了5个。Location返回一个包含5个值的元组,您正试图将其分解为6个值。
playerLocation
和startLocation
之间的区别是什么?playerLocation是玩家在网格上的直接位置,因此每次移动时都会更新。startLocation是一种随机的网格选择,玩家在开始游戏时选择。好吧,理解了它的要点,我创建了第六个元素,称为empty,并将其指定为“0”。我希望保存startLocation,但是我不希望playerLocation被“返回”,因此希望第6个变量被“忽略”。