<p>您希望在函数<code>chooseMonsterClass</code>或<code>fightSequence</code>中的开头使用一个全局声明<code>global Monster</code>。两个都试试(同时)。所以应该是:</p>
<pre><code>import random
def choosePlayerClass():
class Warrior:
health = 100
attack = 10
defense = 10
class Archer:
health = 75
attack = 15
defense = 7
class Mage:
health = 50
attack = 20
defense = 5
playerChoice = input("What class do you want to be? (Warrior, Archer, Mage)? ")
if playerChoice == "Warrior":
Player = Warrior()
elif playerChoice == "Archer":
Player = Archer()
elif playerChoice == "Mage":
Player = Mage()
return Player
def chooseMonsterClass():
global Monster
class Goblin:
health = 25
attack = 10
defense = 5
description = "Goblin"
class Troll:
health = 50
attack = 13
defense = 7
description = "Troll"
class Orc:
health = 75
attack = 15
defense = 10
description = "Orc"
monsterChoice = input("What kind of monster do you want to fight? (Goblin, Troll, Orc)? ")
if monsterChoice == "Goblin":
Monster = Goblin()
elif monsterChoice == "Troll":
Monster = Troll()
elif monsterChoice == "Orc":
Monster = Orc
return Monster
def fightSequence():
global Monster
Player = choosePlayerClass()
Monster = chooseMonsterClass()
encounter = 1
turn = 'player'
while encounter == 1:
if turn == 'player':
action = input("What would you like to do (Attack)? ")
if action == 'Attack':
encounter = humanAttack(Player)
turn = 'monster'
elif turn == 'monster':
encounter = monsterAttack(Monster)
turn = 'player'
fightSequence()
</code></pre>