嵌套';如果';Python中的函数

嵌套';如果';Python中的函数,python,function,nested,Python,Function,Nested,作为Python的新手,我正在做的一件事是角色生成器。从我编写的代码中可以看出,我正在尝试进行种族选择 ########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 wel

作为Python的新手,我正在做的一件事是角色生成器。从我编写的代码中可以看出,我正在尝试进行种族选择

 ########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 hero race Elf行中得到了“预期缩进块”,我想知道如何正确缩进这段代码。谢谢

如果需要评论以外的内容,请使用pass


最好使用字典,而不是嵌套的if语句,即,如果您有一个配置文件键、文本输出、描述的字典,并使用默认值为“受害者”的get,则代码会更干净。

如果您需要注释以外的其他内容,请在必要时使用pass


最好使用字典,而不是嵌套的if语句,即如果您有一个配置文件键、文本输出、描述的字典,并使用默认值为“受害者”的get,那么代码会更干净。

出于某种原因,如果您离开一个块(需要语句的块)空,然后使用
pass
作为占位符。使用注释是行不通的

else:
    pass
发件人:

pass
是一个空操作-当它被执行时,不会发生任何事情。它是 在语法上需要语句时用作占位符, 但不需要执行任何代码,例如:

示例:

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

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

如果yn=='y'或yn=='y':
可以减少为
如果yn.lower()=='y':

出于某种原因,如果要将块(需要语句的块)保留为空,则使用
pass
作为占位符。使用注释是行不通的

else:
    pass
发件人:

pass
是一个空操作-当它被执行时,不会发生任何事情。它是 在语法上需要语句时用作占位符, 但不需要执行任何代码,例如:

示例:

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

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

如果yn=='y'或yn=='y':可以简化为
如果yn.lower()=='y':
为什么不在这样的函数中提取公共内容:

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
就可以重写,但我已经做了很多重构,所以现在就看你了

为什么你不在这样的函数中提取常见的东西:

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
重写,但是我已经做了很多重构,所以现在就看你了

Python中没有
label
goto
。任何自尊的高级编程语言都不应该有这样的东西。这正是OP说忽略它们的原因@Matthias。他说“[…]我现在正试图为python找到label和goto命令”。python中没有
label
goto
。任何自尊的高级编程语言都不应该有这样的东西。这正是OP说忽略它们的原因@Matthias.And(s)他说“[…]我现在正试图为python找到label和goto命令”。