2017-04-18 20 views
0

獲得一個NameError: name 'cost' is not defined at player.reduce_mp(cost)名稱 '成本' 沒有定義錯誤行player.reduce_mp(成本)

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

沒有看到圍繞該區域的代碼,很難說,但作爲'成本'只顯示在'elif'部分,它看起來像是超出了範圍,當你在'player.reduce_mp成本)' – gabe3886

+2

顯示您認爲成本的定義。 –

回答

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

的事情是,你正在使用的 '成本'超出其聲明範圍的價值。 如果最終的if語句是很好縮進,再加入

int cost 

在印刷範圍,其轉讓之前(在之前的elif,無壓痕)。這post將進一步解釋你的這個概念。

+0

''int cost''是無效的python。另外,這裏沒有涉及範圍:if語句(和else/elif)不在python中打開任何範圍。 –

相關問題