[英]How can I print something if an item is in a list?
I am new to python so don't be *facepalm* if you see something that is so easy to solve... 我是python的新手,所以如果您看到容易解决的问题,请不要* facepalm *。
What if
condition I need to use to make the print run in the following bit of code I've tried so far (which doesn't work): if
我需要使用条件来使打印运行到目前为止我尝试过的以下代码(不起作用),该怎么办:
Music=["Play Some Music","Open Music","Music","Play"]
Action=(input("Please Select An Action To Do: "))
if Action==Music:
print("Here Is Your Music")
Since you don't want to check whether the Action is the same as Music list, but only if it is in the Music list, use the in
operator: 由于您不想检查“动作”是否与“音乐”列表相同,而仅当它在 “音乐”列表中时,才可以使用
in
运算符:
if Action in Music:
Also, as mentioned in comments, use lower case for variables in python - action
instead of Action
and music
instead of Music
. 另外,如注释中所述,对于python-
action
而不是Action
和music
而不是Music
,将小写字母用于变量。 It doesn't matter for the interpreter, but this is the convention, and it will make it easier to work with other people, and easier for them to understand and help you. 对于解释器而言,这无关紧要,但这是约定,这将使与其他人的合作更加容易,并且使他们更容易理解和帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.