zwe*_*rg4 -1 python state-machine pycharm python-3.x
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
Run Code Online (Sandbox Code Playgroud)
执行此操作时,出现错误:ValueError: too many values to unpack (expected 2)。我认为错误是return AGILITY, gamedata, strenghtwert在run()班上Strength。
知道有什么问题吗?
成功执行的最后一行strenghtwert = int(input("STRENGTH: >>"))在同一函数中。
如果没有更多信息,例如如何进行调用,这些变量的类型是什么,错误的堆栈跟踪或完整的代码。
多次分配期间通常会发生此错误,在这种情况下,您没有足够的对象分配给变量,或者您分配的对象比变量多。
例如,如果myfunction()返回的迭代器包含三个项目,而不是预期的两个项目,那么您将拥有比分配给变量所需的对象更多的对象。
def myfunction():
return 'stuff', 'and', 'junk'
stuff, junk = myfunction()
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "/test.py", line 72, in <module>
stuff, junk = myfunction()
ValueError: too many values to unpack (expected 2)