0

I am having some trouble with an input question that is supposed to allow the player to choose one of 5 starting races. I used the following code.

def racechoice():
    players.race = input('After you get a chance to think, you choose to be...') #Here race is chosen
    if players.race == "dwarf" or "Dwarf": #Race starting stats
        players.race = "Dwarf"
        players.level = 1
        players.strength = 12
        players.agility = 6
        players.inteligence = 8
        players.vitality = 14
    elif players.race == "Orc" or "orc":
        players.race = "Orc"
        players.level = 1
        players.strength = 14
        players.agility = 10
        players.inteligence = 4
        players.vitality = 12
    elif players.race == "elf" or "Elf":
        players.level = 1
        players.race = "Elf"
        players.strength = 8
        players.agility = 13
        players.inteligence = 12
        players.vitality = 7
    elif players.race == "Human" or "human":
        players.level = 1
        players.race = "Human"
        players.strength = 10
        players.agility = 10
        players.inteligence = 10
        players.vitality = 10
    elif players.race == "gnome" or "Gnome":
        players.race = "Gnome"
        players.strength = 5
        players.agility = 11
        players.intelligence = 17
        players.vitality = 7

When called to display the player's stats:

def stats():
    print(players.name)
    print(players.race)
    print("Level: "+ str(players.level) +" Strength: "+ str(players.strength) +" Agility: " + str(players.agility) +" Inteligence: "+ str(players.inteligence) +" Vitality: "+ str(players.vitality))

It comes back as Dwarf with Dwarf stats no matter what the player chooses. I'm new-ish to Python and was wondering, did I not use the if/elif statement(s) correctly? ex) After you get a chance to think, you choose to be...Orc Ecep Dwarf Level: 1 Strength: 12 Agility: 6 Inteligence: 8 Vitality: 14

WarrenT
  • 4,502
  • 19
  • 27
Ian Moon
  • 3
  • 1
  • 2
  • Really this has to do with understanding how to handle user input with a list of valid options. The game is merely the context you are working in, but it is the concept that you need to understand. – WarrenT Sep 08 '13 at 06:06
  • 1
    If you are going to use tags, please look at the tags available, and read what they mean on this site, which is for professional and "enthusiast" programmers. RPG is one of the most widely used languages among professional programmers, running core business applications behind the scenes at more companies than most people realize. – WarrenT Sep 08 '13 at 06:11

2 Answers2

0

There is a problem, you should do:

if players.race == "dwarf" or "players.race == Dwarf":

But even better, change your conditions to:

elif players.race.lower() == "orc":

It is also recommended that you use a class to encapsulate functionality, for example:

class Player(object):
    def __init__(self, race, level, strength, agility, inteligence, vitality):
        self.race = race
        self.level = level
        self.strength = strength
        self.agility = agility
        self.inteligence = inteligence
        self.vitality = vitality

player = Player('Dwarf', 1, 12, 6, 8, 14)

then your racechoice function will look so:

def racechoice():
    race = input('After you get a chance to think, you choose to be...')
    if race.lower() == 'dwarf':
        player = Player('Dwarf', 1, 12, 6, 8, 14)
    elif race.lower() == 'orc':
        player = Player('Orc', 1, 14, 10, 4, 12)
    ...
    return player
elyase
  • 39,479
  • 12
  • 112
  • 119
0

You can't use or in that way. All your if/elif statements should be of the form:

if players.race == "dwarf" or players.race == "Dwarf":

"Dwarf" all by itself is considered True by Python and empty strings "" are considered False, so your first if always succeeds.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • That worked, and got the proper output. After you get a chance to think, you choose to be...Orc Ecep Orc Level: 1 Strength: 14 Agility: 10 Inteligence: 4 Vitality: 12 – Ian Moon Sep 08 '13 at 02:38