ShadowKiller
ShadowKiller

Reputation: 1

Nesting 'If' Functions in Python

As a newbie to Python, one thing I'mm working on is a character generator. As you'll be able to see from the code I've thrown together, I'm trying to make a race selection.

 ########Race########
#.racechoose (label)
hero_race = input("What is your hero's race? (Human / Elf / Dwarf / Orc) Don't forget to capitalize! ")
if hero_race == 'Human':
    print ("Humans are well-rounded, average characters. They have a bonus of + 1 to Speed and + 1 to Int.")
    yn = input ("Do you want to be a Human (Y/N)? ")
    if yn == 'y' or yn == 'Y':
        profile['Race'] = "Human"
        print ("Your hero", profile['Name'], "is a human.")
    else:
        #goto racechoose

elif hero_race == 'Elf':
    print("Elves are very fast, and they have a bonus of + 2 to Speed.")
    yn = input("Do you want to be an Elf? (y/n) ")
    if yn == 'y' or yn == 'Y':
        profile['Race'] = "Elf"
        print("Your hero ", profile['Name'], "is an Elf.")
    else:
        #goto racechoose

elif hero_race == 'Dwarf':
    print("Dwarves are small, but strong. Dwarves get a bonus of + 2 Muscle.")
    yn = input("Do you want to be a Dwarf? (Y/N) ")
    if yn == 'y' or yn =='Y':
        profile['Race'] = 'Dwarf'
        print("Your hero ", profile['Name'], "is a Dwarf.")
    else:
        #goto racechoose

else: #orc
    print("Orcs are brute muscle. Orcs get a bonus of + 3 to Muscle, but - 1 to Int.")
    yn = input("Do you want to be an Orc? (Y/N) ")
    if yn == 'y' or yn == 'Y':
        profile['Race'] = 'Orc'
        print("Your hero ", profile['Name'], "is an Orc.")
    else:
        #goto racechoose

Please ignore the goto and label comments - I've just recently stopped using blitzbasic and am now trying to find label and goto commands for python.

Anyway, I get "Expected an indented block" on the elif hero race Elf line, and I'd like to know how to indent this code correctly. Thanks!

Upvotes: 0

Views: 276

Answers (3)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

For some reason if you're leaving a block(block where a statement is required) empty then use pass as a placeholder there. Using comments won't work.

else:
    pass

From docs:

pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example:

Examples:

def f(arg): pass    # a function that does nothing (yet)

class C: pass       # a class with no methods (yet)

And if yn == 'y' or yn == 'Y': can be reduced to if yn.lower() == 'y':

Upvotes: 2

J0HN
J0HN

Reputation: 26941

Why wouldn't you just extract the common stuff in a functions like this:

def get_race_description(race):
    return {
        'human': "Humans are well-rounded ...",
        'elf': "Elves are very fast ...",
         # all the other race descriptions
    }.get(race.lower(), "WTF? I don't know that race")

def confirm_race_selection(race):
    print (get_race-description(race))
    yn = input ("Do you want to be {0} (Y/N)? ".format(race))
    return (yn.lower() == 'y')

while True:
    hero_race = input("What is your hero's race?")
    if (confirm_race_selection(hero_race)):
        profile['Race'] = hero_race
        print ("Your hero {0} is {2}".format(profile['Name'], hero_race))
        break

This snippet can be rewritten not using break at all, but I've done whole lot of refactoring already so it's up to you now

Upvotes: 1

Steve Barnes
Steve Barnes

Reputation: 28380

You need something on an else other than a comment, use pass if you must.

Better would be to use a dictionary instead of nested if statements, i.e. if you have a dictionary of profile keys, text outputs, descriptions and use get with a default of 'victim' the code would be a lot cleaner.

Upvotes: 0

Related Questions