1
votes

I'm trying to build a simple Fight Sequence, where a user pick's their "Class" (Warrior, Archer, Mage), as well as the monster they want to fight (Goblin, Troll, Orc).

The code I have so far is:

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

And I get this error:

Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py", line 109, in File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py", line 102, in fightSequence File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py", line 63, in humanAttack builtins.NameError: global name 'Monster' is not defined

Thanks!

2
the Orc is missing parentheses, just a FYIVoronoiPotato
Sounds like you have an error in your humanAttack method. But how curious, there's no humanAttack method defined in all the code you have so far! Are you holding out on us?Kevin
See: jsfiddle.net/EM4AA/4 Archer OP!Shmiddty
Found the missing parenthese, danke for that.Keyfer Mathewson
Archer is always OP, because Legolas is my favorite. And I was holding out on you @Kevin. I'm so sorry, but it's all fixed now!Keyfer Mathewson

2 Answers

4
votes

Here you initialize the Monster variable:

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

but what if none of those are true and none of the if statements are entered? You should set your variable with a default value before the if statements so that the case where some nonsense is entered by the user is handled:

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

Better yet put the whole thing in a loop and ask the user to enter a valid race when a nonsense race is given.

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."
0
votes

You want a global declaration, global Monster at the beginning, in either the function chooseMonsterClass or in fightSequence. Try both (at once). so it should be:

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