0

Python の初心者として、私が取り組んでいることの 1 つは、文字ジェネレーターです。一緒に投げたコードからわかるように、私はレースの選択をしようとしています。

 ########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

goto と label のコメントは無視してください - 最近 blitzbasic の使用をやめたばかりで、現在 Python の label と goto コマンドを探しています。

とにかく、elif のヒーロー レース Elf の行で "Expected an indented block" が表示されます。このコードを正しくインデントする方法を知りたいです。ありがとう!

4

3 に答える 3

1

次のような関数で一般的なものを抽出しないのはなぜですか。

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

このスニペットはまったく使用せずに書き直すことができますbreakが、私はすでに多くのリファクタリングを行っているので、今はあなた次第です

于 2013-06-25T17:51:40.493 に答える