【问题标题】:I'm trying to make a simple story maker but I'm stuck on the gender part我正在尝试制作一个简单的故事制作者,但我被困在性别部分
【发布时间】:2021-01-28 13:30:58
【问题描述】:

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

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)

【问题讨论】:

  • (not False) 不等于 False;它等于 True
  • 你的 isMale 函数永远不会被调用。另外,你可能不想递归调用它,所以你最好去掉 return isMale() ,这将在你函数的无休止调用中结束.
  • 你为什么不在Male == "True"内部处理你的逻辑?

标签: python if-statement boolean


【解决方案1】:

我认为您正在寻找类似的东西。

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)

【讨论】:

  • 非常感谢您,好心的陌生人,我最近开始学习python,所以我还很新。成功了!
  • 虽然您的代码回答了这个问题,但您并没有真正解释为什么原始代码是错误的(您添加了两个 cmets,但它们只说明了代码在做什么,而不是为什么)。最好添加解释为什么您的解决方案有效。
  • 你是对的。我认为使用 cmets 可以轻松识别问题的根源。它们是基本的 Python 原则......
【解决方案2】:

首先,为什么你的函数会返回自己?你为什么不把这一切都说出来?您刚刚定义了一个函数并编写了它,但是您并没有对它做任何事情。

添加

if __name__ == "__main__":
    isMale()

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

此外,没有必要将 bool 与 True/False 进行比较。随便写:

if Male_Bool:

elif (not Male_Bool) == False:

错了。如果您选择男性,那么 Male_Bool 将设置为 False,此 elif 将评估为(非 False)== False。它永远不会运行。如果您只有男性/女性选项,只需将其替换为 else 即可。

【讨论】:

    【解决方案3】:

    好的,首先我们需要了解问题,您编写的代码和它的工作方式只是对函数的工作方式感到困惑。让我解释一下结构。

    def functionName(arguments):
       process
    return output
    

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

    functionName(args)
    

    【讨论】:

    • 感谢您向我解释!不知道写完之后还要调用它。
    【解决方案4】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2011-09-16
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      相关资源
      最近更新 更多