简体   繁体   English

ValueError:太多值无法解包(预期2)PYTHON

[英]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) . 执行此操作时,出现错误: ValueError: too many values to unpack (expected 2) I think the error is in return AGILITY, gamedata, strenghtwert in run() in the class Strength . 我认为错误是在Strength类中的run()return AGILITY, gamedata, strenghtwert

Any idea what's the problem? 知道有什么问题吗?

The last line that is successfully executed is strenghtwert = int(input("STRENGTH: >>")) in the same function. 成功执行的最后一行是同一函数中的strenghtwert = int(input("STRENGTH: >>"))

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. 例如,如果myfunction()返回的迭代器包含三个项目,而不是预期的两个项目,那么您将拥有比分配给变量所需的对象更多的对象。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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