我在这里做错了什么,我该怎么做?

时间:2015-03-13 23:48:11

标签: undefined python-3.4 function

def movedamage:
    if Move=="Bop":
        MoveDamage=60
    if Move=="Bim":
        MoveDamage=60
    if Move=="Bam":
        MoveDamage=60
    if Move=="Pow":
        MoveDamage=60
print(Enemy)
print(Enemypokemon)
print ("A wild " + (Enemypokemon) + " has appeared")
print ("what would you like to do?")
molly=input ("1.Battle\n2.Switch pokemon")
if molly=="2":
    for pokemon in PokemonInventory:
        print (pokemon)
if molly=="1":
    Pokehealth=(operator.itemgetter(4)(Currentpokestats))
    Enemypokehealth=(operator.itemgetter(4)(Enemypokestats))
    print("Enemy pokemon health: "+ Enemypokehealth)
    print("Your pokemon health: "+ Pokehealth )
    Move=input ("Select move\n" +CurrentMoveset)


    EnemyHealth=operator.itemgetter(4)(Enemypokestats)
    Attack=operator.itemgetter(0)(Currentpokestats)
    Totaldamage=(int((int(Attack))*int((MoveDamage)))/60)
    Enemypokehealth=(int(Enemypokehealth))-Totaldamage
    print("Enemy pokemon health:" +str(Enemypokehealth))
    print("You did " + str(Totaldamage)+ " with " +(Move))

当我输入BopBimBamPow时,我收到错误消息:

"Totaldamage=(int((int(Attack))*int((MoveDamage)))/60)
NameError: name 'MoveDamage' is not defined"

这让我感到困惑,因为我认为我已经做到了这样,以及#34; MoveDamage"如果我使用任何先前的输入,将是60。但是,我得到了所有这些错误。

2 个答案:

答案 0 :(得分:3)

Move未在movedamage中定义,您从未设置MoveDamage。而是传递一个参数,并返回一个值。

def moveDamage(move):
    if move in ["Bop", "Bim", "Bam", "Pow"]:
        return 60
    else:
        return 0

然后使用

moveDamage(Move)

此外,python区分大小写,但这并不意味着您应该为函数movedamage和变量MoveDamage命名。它使代码混乱且难以理解。

答案 1 :(得分:2)

首先,请添加一个python标签。其次,没有定义MoveDamage的原因是缩进使得你的函数movedmage在if Move ==" Pow"之后结束。这意味着后来对MoveDamage的调用超出了范围。

我不知道你打算如何打电话,但你应该使用更像这样的东西:

def movedamage (move) :
    if move == "Bop" :
......

#then call with
damage = movedamage("Bop")`