简体   繁体   中英

ValueError: too many values to unpack (expected 2) PYTHON

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

When I execute this, I get the error: ValueError: too many values to unpack (expected 2) . I think the error is in return AGILITY, gamedata, strenghtwert in run() in the class Strength .

Any idea what's the problem?

The last line that is successfully executed is strenghtwert = int(input("STRENGTH: >>")) in the same function.

Without more information such as how the call was made, what types some of those variables are, the stack trace of the error, or your complete code.

This error typically occurs during a multiple-assignment where you either don't have enough objects to assign to the variables or you have more objects to assign than variables.

If for example myfunction() returned an iterable with three items instead of the expected two then you would have more objects than variables necessary to assign to.

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)

This works the other way around where you have more variables than objects.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM