名称'cost'未定义错误行player.reduce_mp(cost)

时间:2017-04-18 13:22:51

标签: python python-3.x

获得NameError: name 'cost' is not defined at player.reduce_mp(cost)

elif index == 1:
    player.choose_magic()
    magic_choice = int(input('Choose magic'))-1
    magic_dmg = player.generate_spell_damage()
    spell = player.get_spell_name(magic_choice)
    cost = player.get_spell_mp_cost(magic_choice)
    current_mp = player.get_mp()

    if cost > current_mp:
        print(bcolors.FAIL + '\nNot enough MP\n' + bcolors.ENDC)
        continue

player.reduce_mp(cost)
enemy.take_damage(magic_dmg)
print(bcolors.OKBLUE + '\n' + spell + 'deals', str(magic_dmg), 'points of damage' + bcolors.ENDC)

2 个答案:

答案 0 :(得分:0)

现在怎么样:

elif index==1:
    player.choose_magic()
    magic_choice=int(input('Choose magic'))-1
    magic_dmg=player.generate_spell_damage()
    spell=player.get_spell_name(magic_choice)
    cost=player.get_spell_mp_cost(magic_choice)

    current_mp=player.get_mp()

    if cost>current_mp:
        print(bcolors.FAIL+ '\nNot enough MP\n'+bcolors.ENDC)
        continue

    player.reduce_mp(cost)
    enemy.take_damage(magic_dmg)
    print(bcolors.OKBLUE+'\n'+spell+'deals',str(magic_dmg),'points of damage'+bcolors.ENDC)

答案 1 :(得分:-1)

问题是你在声明范围之外使用'cost'值。 如果您的最终if语句缩进,则添加

int cost

在打印范围内,在其分配之前(在elif之前,没有缩进)。这个post将进一步向您解释这个概念。