ValueError:解压缩的值太多(预期为2)PYTHON

时间:2017-10-04 19:11:12

标签: python python-3.x pycharm state-machine

class Strength(State):
    def run(self, gamedata):
        print("You have 100 points to assign to your character.\n Start now to assign those Points to your characters strength, agility, speed and defense.")
        strenghtwert = int(input("STRENGTH: >>"))
        return AGILITY, gamedata, strenghtwert

    def next(self, next_state):
        if next_state == AGILITY:
            return CreatePlayer.agility

class Agility(State):
    def run(self, gamedata,strenghtwert):
        agilitywert = int(input("AGILITY: >>"))
        return SPEED, gamedata, strenghtwert, agilitywert

    def next(self, next_state):
        if next_state == SPEED:
            return CreatePlayer.speed

当我执行此操作时,我收到错误:ValueError: too many values to unpack (expected 2)。 我认为错误发生在班级return AGILITY, gamedata, strenghtwert的{​​{1}} run()中。

知道问题是什么?

在同一个函数中成功执行的最后一行是Strength

1 个答案:

答案 0 :(得分:5)

没有更多信息,例如调用的方式,这些变量的类型,错误的堆栈跟踪或完整的代码。

此错误通常发生在多次分配期间,您要么没有足够的对象来分配变量,要么分配的对象多于变量。

例如,如果myfunction()返回一个包含三个项而不是预期的两个项的iterable,那么你将拥有比分配所需的变量更多的对象。

def myfunction():
    return 'stuff', 'and', 'junk'

stuff, junk = myfunction()

Traceback (most recent call last): File "/test.py", line 72, in <module> stuff, junk = myfunction() ValueError: too many values to unpack (expected 2)

这反过来可以使用比对象更多的变量。

def myfunction():
    return 'stuff'

stuff, junk = myfunction()

Traceback (most recent call last): File "/test.py", line 72, in <module> stuff, junk = myfunction() ValueError: too many values to unpack (expected 2)