我正在尝试制作一个简单的故事制作者,但我被困在性别部分

I'm trying to make a simple story maker but I'm stuck on the gender part

我正在尝试制作一个包含用户输入的简单故事,但我无法让我的性别部分发挥作用。我希望它能够从输入中识别性别,然后打印出适当的代词。

import time
print("What's your name?")
character_name = input("Type it in here: ")
print("How old are you?")
character_age = input("Type your age in here here: ")
def isMale():
    Male = input("You're a male? True or False?")
    if (Male == "True"):
        Male_Bool = True
    elif (Male == "False"):
        Male_Bool = False
    if (Male_Bool) == True:
        print("There was a man named " + character_name + ", ")
        print("he was " + character_age + ".")
        print("He likes the name " + character_name + ", ")
        print("but didn't like being " + character_age + ".")
    elif(not Male_Bool) == False:
        print("There was a woman named " + character_name + ", ")
        print("she was " + character_age + ".")
        print("She likes the name " + character_name + ", ")
        print("but didn't like being " + character_age + ".")
    return isMale()
time.sleep(10)

我认为您正在寻找这样的东西。

import time
print("What's your name?")
character_name = input("Type it in here: ")
print("How old are you?")
character_age = input("Type your age in here here: ")
def isMale():
    Male = input("You're a male? True or False?")
    if (Male == "True"):
        Male_Bool = True
    elif (Male == "False"):
        Male_Bool = False
    if (Male_Bool) == True:
        print("There was a man named " + character_name + ", ")
        print("he was " + character_age + ".")
        print("He likes the name " + character_name + ", ")
        print("but didn't like being " + character_age + ".")
    #Check if Male_Bool is False
    elif(Male_Bool) == False:
        print("There was a woman named " + character_name + ", ")
        print("she was " + character_age + ".")
        print("She likes the name " + character_name + ", ")
        print("but didn't like being " + character_age + ".")
    return 
#Call the function
isMale()
time.sleep(10)

另请查看您重构的代码。

import time

def isMale():
    Male = input("You're a male? True or False?")
    if (Male == "True"):
        Male_bool = True
    else:
        Male_bool = False
    return Male_bool

character_name = input("What's your name?")
character_age = input("How old are you?")
male = isMale()
if male:
    print(f"There was a man named {character_name},")
    print(f"he was {character_age}.")
    print(f"He likes the name {character_name},")
    print(f"but didn't like being {character_age}.")
else:
    print(f"There was a woman named {character_name},")
    print(f"she was {character_age}.")
    print(f"She likes the name {character_name},")
    print(f"but didn't like being {character_age}.")

time.sleep(10)

首先,为什么你的函数returns本身?你为什么不全部调用呢?您刚刚定义了一个函数,并编写了它,但是您没有对它执行任何操作。

添加

if __name__ == "__main__":
    isMale()

在底部。它会调用你的函数。

此外,不需要将 bool 与 True/False 进行比较。随便写:

if Male_Bool:

elif (not Male_Bool) == False:

错了。如果您选择男性,那么 Male_Bool 将被设置为 False,此 elif 将计算为 (not False) == False。它永远不会 运行。如果您只有 male/female 个选项,只需将其替换为 else。

好吧,首先我们需要了解问题所在,您编写的代码及其工作方式与功能的工作方式有很大的不同。让我解释一下结构。

def functionName(arguments):
   process
return output

所以当我们写完我们的函数 现在我们必须调用它 运行 像这样

functionName(args)

首先你没有在你的程序中调用 isMale() 函数,第二行 elif(not Male_Bool) == False: 你正在否定一个具有“False”值的属性,使他等于为真。最后,我不明白你想要什么 return,我建议你删除 return 行。

import time
print("What's your name?")
character_name = input("Type it in here: ")
print("How old are you?")
character_age = input("Type your age in here here: ")
def isMale():
    Male = input("You're a male? True or False?")
    if (Male == "True"):
        Male_Bool = True
    elif (Male == "False"):
        Male_Bool = False
    if (Male_Bool) == True:
        print("There was a man named " + character_name + ", ")
        print("he was " + character_age + ".")
        print("He likes the name " + character_name + ", ")
        print("but didn't like being " + character_age + ".")
    elif(Male_Bool) == False:
        print("There was a woman named " + character_name + ", ")
        print("she was " + character_age + ".")
        print("She likes the name " + character_name + ", ")
        print("but didn't like being " + character_age + ".")

isMale()

time.sleep(10)