【发布时间】: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