未定义全局变量,但之前已定义

问题描述 投票:0回答:1

我从错误的定义中得到的代码:

def preFight():
    global enemy
    enemynum=random.randint(1,3)
    if enemynum==1:
        enemy=goblinIG
    elif enemynum==2:
        enemy=zombieIG
    elif enemynum==3:
        enemy==skeletonIG
    fight()

def fight():
    print "%s    vs    %s" % (playerIG.name, enemy.name)
    print "%s Health: %d/%d        &s's Health: %i/%i" % (playerIG.name,playerIG.health, playerIG.maxhealth, enemy.name, enemy.health, enemy.maxhealth)
    print "Potions: %s" % (playerIG.pots)
    print "1.) Attack"
    print "2.) Drink Potion"
    print "3.) Run"
    option=raw_input("-->")
    if option=="1":
        attack()
    elif option=="2":
        drinkpot()
    elif option=="3":
        run()
    else:
        fight()

def attack():
    os.system('clear')
    pAttack=random.randint(playerIG.attack/2, playerIG.attack)
    eAttack=random.randint(enemy.attack/2, enemy.attack)
    if pAttack()==playerIG.attack/2:
        print "You miss!"
    else:
        enemy.health-=pAttack
        print "You deal %s damage" % pAttack
    option=raw_input("")
    if enemy.health<=0:
        win()
    os.system('clear')
    if eAttack()==enemy.attack/2:
        print "The enemy missed!"
    else:
        playerIG.health-=eAttack
        print "The enemy deals %s damage" % eAttack
    option=raw_input("")
    if playerIG.health<=0:
        die()
    else:
        fight()

def drinkpot():
    os.system('clear')
    if playerIG.pots==0:
        print "You don't have any potions!"
        option=raw_input("")
        fight()
    else:
        playerIG.health+=50
        if playerIG.health>playerIG.maxhealth:
            playerIG.health=playerIG.maxhealth
        print "You drank a potion"
    option=raw_input("")
    fight()

def run():
    pAttack=random.randint(playerIG.attack/2, playerIG.attack)
    eAttack=random.randint(enemy.attack/2, enemy.attack)
    os.system('clear')
    runnum=random.randint(1,3)
    if runnum==1:
        print "You successfully ran away!"
        option=raw_input("")
        start1()
    else:
        print "You failed to get away!"
        option=raw_input("")
        os.system('clear')
        if eAttack()==enemy.attack/2:
            print "The enemy missed!"
        else:
            playerIG.health-=eAttack
            print "The enemy deals %s damage" % eAttack
    option=raw_input("")
    if playerIG.health<=0:
        die()
    else:
        fight()

def win():
    print "You have successfully killed the %s!" % enemy.name
    print "You have gained &s gold!" % enemy.goldgain

def die():
    print "You have died whilst trying to defeat %s" % enemy.name
    print "1.) Try again"
    print "2.) Quit"
    option=raw_input("-->")
    if option=="1":
        start()
    elif option=="2":
        sys.exit()
    else:
        die()

#def store()


main()

战斗开始时出现的错误如下:

Traceback (most recent call last):
  File "/home/ubuntu/workspace/rpg_game.py", line 194, in <module>
    main()
  File "/home/ubuntu/workspace/rpg_game.py", line 46, in main
    start()
  File "/home/ubuntu/workspace/rpg_game.py", line 60, in start
    start1()
  File "/home/ubuntu/workspace/rpg_game.py", line 75, in start1
    preFight()
  File "/home/ubuntu/workspace/rpg_game.py", line 93, in preFight
    enemy==skeletonIG
NameError: global name 'enemy' is not defined

当我定义敌人并将其设置为全局时,这使我感到困惑。有什么问题吗?

python variables traceback
1个回答
0
投票

此功能分配有误:

def preFight():
    global enemy
    enemynum=random.randint(1,3)
    if enemynum==1:
        enemy=goblinIG
    elif enemynum==2:
        enemy=zombieIG
    elif enemynum==3:
        enemy==skeletonIG
    fight()

[enemy==skeletonIG应该为enemy=skeletonIG,因为==检查是否相等,而=是赋值运算符。

© www.soinside.com 2019 - 2024. All rights reserved.