Skip to content Skip to sidebar Skip to footer

Python 3 Input And If Statement

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.rac

Solution 1:

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:

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

Solution 2:

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.

Post a Comment for "Python 3 Input And If Statement"