gender = input("What is your gender? ")
if 'male' in gender:
print("You are a boy!")
if 'female' in gender:
print("You are a girl!")
我可以问一下如何做到这一点,以便" 男性 "不会被视为" 女性 "
的一部分它会一直打印" 你是个男孩!"
答案 0 :(得分:4)
要获得结果,如上所述编辑代码,如下所示:
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
答案 1 :(得分:4)
首先,检查“女性”状况。
然后使用elif语句检查“男性”状况。
如果第一个语句为true,则不会检查第二个语句。
gender = input("What is your gender? ")
if 'female' in gender:
print("You are a girl!")
elif 'male' in gender:
print("You are a boy!")
答案 2 :(得分:1)
许多可能性:
_1
或
gender = input("What is your gender? ")
if 'female' in gender:
print("You are a girl!")
else:
print("You are a boy!")
或
enter code here
if gender == 'male':
print("You are a boy!")
if gender == 'female':
print("You are a girl!")
清理inout to lower string
可能是一个好主意if gender == 'male':
print("You are a boy!")
elif gender == 'female':
print("You are a girl!")
else:
print("You seem to be a magical dragon")
答案 3 :(得分:0)
import re
gender = input("What is your gender? ")
if re.findall(r'\bmale\b', gender):
print("You are a boy!")
elif re.findall(r'\bfemale\b', gender):
print("You are a girl!")
示例输出:
What is your gender? >? i am male
You are a boy!
What is your gender? >? i am female
You are a girl!
What is your gender? >? male
You are a boy!
What is your gender? >? female
You are a girl!
What is your gender? >? maleAndFemale
What is your gender?