1

私は、ユーザーが自分の「クラス」(戦士、射手、魔道士)と、戦いたいモンスター(ゴブリン、トロール、オーク)を選ぶ、単純な戦闘シーケンスを構築しようとしています。

私がこれまでに持っているコードは次のとおりです。

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():

    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():
    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()

そして、私はこのエラーを受け取ります:

トレースバック(最後の最後の呼び出し):ファイル "C:\ Program Files(x86)\ Wing IDE 101 4.1 \ src \ debug \ tserver_sandbox.py"、行109、ファイル "C:\ Program Files(x86)\ Wing IDE 101 4.1 \ src \ debug \ tserver_sandbox.py "、102行目、fightSequenceファイル" C:\ Program Files(x86)\ Wing IDE 101 4.1 \ src \ debug \ tserver_sandbox.py "、63行目、humanAttackbuiltins.NameError :グローバル名「モンスター」が定義されていません

ありがとう!

4

2 に答える 2

4

ここでは、Monster 変数を初期化します。

if monsterChoice == "Goblin":
    Monster = Goblin()
elif monsterChoice == "Troll":
    Monster = Troll()
elif monsterChoice == "Orc":
    Monster = Orc()

しかし、それらのどれも真ではなく、if ステートメントのどれも入力されていない場合はどうなるでしょうか? ユーザーがナンセンスを入力した場合に対処できるように、if ステートメントの前に変数にデフォルト値を設定する必要があります。

Monster = DefaultRace
if monsterChoice == "Goblin":
    Monster = Goblin()
elif monsterChoice == "Troll":
    Monster = Troll()
elif monsterChoice == "Orc":
    Monster = Orc()

すべてをループに入れて、無意味なレースが行われたときに有効なレースに参加するようにユーザーに依頼することをお勧めします。

while True:
    monsterChoice = input("What kind of monster do you want to fight? (Goblin, Troll, Orc)? ")
    if monsterChoice in ["Goblin","Troll","Orc"]:
        break
    else:
        print "Unrecognized race requested, please select one of Goblin, Troll, Orc."
于 2013-01-09T18:27:12.197 に答える
0

global Monster関数chooseMonsterClassまたはのいずれかで、最初にグローバル宣言が必要ですfightSequence。両方を (一度に) 試してください。したがって、次のようになります。

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()
于 2013-01-11T05:21:14.597 に答える