如何使它成为'男性"不计入女性"?

时间:2017-12-02 09:31:45

标签: python-3.x if-statement

gender = input("What is your gender? ")
if 'male' in gender:
    print("You are a boy!")
if 'female' in gender:
    print("You are a girl!")

我可以问一下如何做到这一点,以便" 男性 "不会被视为" 女性 "

的一部分

它会一直打印" 你是个男孩!"

4 个答案:

答案 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!")

1

示例输出:

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?