全局变量未定义,尽管之前已定义

Global variable not defined although was defined earlier

我从变量的 error/defining 得到的代码:

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

这让我很困惑,因为我定义了 enemy 并将其设置为全局。有什么问题?

您在此函数中的赋值错误:

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,因为 == 检查相等性而 = 是赋值运算符。