Python3:ValueError:需要多于1个值才能解压缩

时间:2015-10-11 23:43:08

标签: python

我目前正在开发基于文本的游戏,我试图从函数中返回两个单独的值,特别是函数的这个块:

if userhp > 0 and enemyhp <= 0:
    print(enemyname + ' has been defeated!')
    lootdrop = loot_generator()
    print(lootdrop)
    restart = input('Restart? Y/N ')
    if restart.lower() not in ['y', 'n']:
        while restart.lower() not in ['y', 'n']:
            restart = input('Invalid input. Restart? Y/N ')
    return(restart, lootdrop)

这是我收到错误的块:

else:
    userhp = default_userhp
    damagebuff = default_damagebuff
    restart, lootdrop = normal_ai(userhp, damagebuff) # <-- **error here**
    if lootdrop == '0':
    damagebuff += 1
    elif lootdrop == '1':
        default_userhp += 2
    if restart.lower() in ['y', 'yes']:
        ...

根据评论者的要求,这里是整个normal_ai函数(不知道为什么定义它之后的whitespacing缺失,但它在普通代码中存在):

def normal_ai(playerhp, damagebuff):

enemyname = all_enemy_names[random.randint(0,2)]
enemyhp = random.randint(5,13)
userhp = playerhp
iselite = 0

if enemyhp in [11, 12, 13]:
    enemyname = 'Elite ' + enemyname
elif enemyhp == 5:
    enemyname = 'Weakling ' + enemyname

while enemyhp > 0 and userhp > 0:
    userdmg = random.randint(0,5) + damagebuff
    damage = random.randint(0,2)
    if iselite == 1:
        hitchance = random.randint(0,1)
        if hitchance == 1:
            damage += 1
    if True:
        print('')
        if damage == 0:
            print(enemyname + ' missed.')
        else:
            print(enemyname + ' strikes you for ' + str(damage) + ' HP!')
            time.sleep(0.5)
            userhp -= damage
            print('You have ' + str(userhp) + ' HP left.')
        print('')
        action = input('ATTACK or FLEE? ')
        if action.lower() not in ['attack', 'flee']:
            while action.lower() not in ['attack', 'flee', 'wait']:
                action = input('Invalid input. ATTACK or FLEE? ')
        if action.lower() == 'attack':
            enemyhp -= userdmg
            print('You do ' + str(userdmg) + ' to the ' + enemyname +'!')
            time.sleep(0.5)
            print(enemyname + ' has ' + str(enemyhp) + ' HP left.')
        elif action.lower() == 'flee':
            fleechance = random.randint(0,3)
            if fleechance == 0:
                print('You fail to escape!')
                time.sleep(0.5)
            elif userhp <= 0:
                print('You are too wounded to run!')
                time.sleep(0.5)
            else:
                print('You successfully escape!')
                time.sleep(0.5)
                break #END
        elif action.lower() == 'wait':
            print('You wait to see the ' + enemyname + "'s next move.")
            time.sleep(0.5)

if userhp > 0 and enemyhp <= 0:
    print(enemyname + ' has been defeated!')
    loot_generator()
    restart = input('Restart? Y/N ')
    if restart.lower() not in ['y', 'n']:
        while restart.lower() not in ['y', 'n']:
            restart = input('Invalid input. Restart? Y/N ')
    return(restart)
elif userhp <= 0 and enemyhp > 0:
    print('You have been struck down by ' + enemyname + '!')
    restart = input('Restart? Y/N ')
    if restart.lower() not in ['y', 'n']:
        while restart.lower() not in ['y', 'n']:
            restart = input('Invalid input. Restart? Y/N ')
    return(restart)
else:
    print('Somehow... You both killed eachother!')
    restart = input('Restart? Y/N ')
    if restart.lower() not in ['y', 'n']:
        while restart.lower() not in ['y', 'n']:
            restart = input('Invalid input. Restart? Y/N ')
    return(restart)

4 个答案:

答案 0 :(得分:1)

如果您的第一个if条件if userhp > 0 and enemyhp <= 0:的计算结果为false,那么您的函数将返回None,这不是2元组,因此无法解压缩。

答案 1 :(得分:0)

错误意味着你正在做这样的事情:

foo, bar = some_function()

...但some_function()只返回一个项目。 &#34; ...需要多件商品才能打开&#34;表示解压缩(将结果拆分为两个或多个变量)仅#34;解压缩&#34;一个项目。

在您的情况下,这意味着normal_ai(...)可能无法返回您认为它返回的内容。简单的打印声明可以帮助验证您的假设。

答案 2 :(得分:0)

问题出在normal_ai,错误可以这样复制:

def foo(one,two):
    return one,

a,b = foo('bar','baz')

上一个可能会导致此错误:

Traceback (most recent call last):   
File "C:/Users/Leb/Desktop/Python/test2.py", line 4, in <module>
    a,b = foo('bar','baz') ValueError: need more than 1 value to unpack

答案 3 :(得分:-1)

如果return语句为normal_aireturn(restart, lootdrop),那么通过此语句,您将返回一个元组[一个值]。省略return语句中的括号。