0

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!

3
  • There is no label and goto in Python. No self-respecting higher level programming language should have something like that. Commented Jun 25, 2013 at 17:49
  • Which is exactly why the OP said to ignore them @Matthias. Commented Jun 25, 2013 at 17:50
  • 1
    And (s)he said "[...] am now trying to find label and goto commands for python". Commented Jun 25, 2013 at 17:51

3 Answers 3

2

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':

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.