简体   繁体   English

!=后只能有一个字符串吗?

[英]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.

相关问题 找到k个连续数字后如何终止字符串? - How can you terminate a string after k consecutive numbers have been found? Photologue中的画廊只能有一张照片 - Gallery in Photologue can have only one Photo 弹出窗口只能有一个小部件作为内容 - Popup can have only one widget as content 终端中可以有多个python吗? - Can you have more than one python in terminal? 您可以为 Python 枚举属性提供一个空字符串吗? - Can you have an empty string for a Python enum attribute? Python:当你只有方法的字符串名称时,如何调用方法? - Python: How do you call a method when you only have the string name of the method? sqlite3.Warning: 一次只能执行一条语句 - sqlite3.Warning: You can only execute one statement at a time 你能从 Django/Python 的图片库中只引用一个吗? - Can you reference only one from image gallery for Django/Python? 您可以删除 pandas pivot 表中仅一个 aggfunc 的重复项吗? - Can you drop duplicates for only one aggfunc in a pandas pivot table? 您有一个仅由数字组成的字符串。 您需要找出给定字符串开头有多少个零数字 ("0") - You have a string that consist only of digits. You need to find how many zero digits ("0") are at the beginning of the given string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM