Python 3 input and if statement

夙愿已清 提交于 2019-12-12 00:44:15

问题


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


回答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:

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



回答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.



来源:https://stackoverflow.com/questions/18679778/python-3-input-and-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!