[英]Can you only have one string after !=
I'm just writing some small code and I'm not able to make this work, I am only able to have one string which is eg boy or girl but I would like to have both, do I have to make a separate elif statement or can I somehow put girl in there as well 我只是写一些小代码,但我无法完成这项工作,我只能使用一个字符串,例如,男孩或女孩,但是我想同时使用这两个字符串,是否需要另外编写一个elif语句或者我也可以以某种方式把女孩放在那里
gender = input("Are you a boy or a girl? : ")
if (gender != "boy"):
print("Get out of here you mutant")
exit()
You would need to have 3 conditions: 您需要具备3个条件:
if gender == 'boy':
print ('You are a boy')
elif gender == 'girl':
print ('You are a girl')
else:
print("Get out of here you mutant")
Or you can do like that: 或者您可以这样做:
if gender in ('boy', 'girl'):
print('You are a {}'.format(gender)
else:
print("Get out of here you mutant")
In the second script you check if gender
is a girl or a boy and if so it puts gender
variable in the print output string. 在第二个脚本中,您检查gender
是女孩还是男孩,如果是,则将gender
变量放入打印输出字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.