简体   繁体   English

如果项目在列表中,如何打印内容?

[英]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而不是Actionmusic而不是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.

相关问题 我怎样才能得到它打印东西检查证明为虚假列表中的每一个项目是什么时候? - How can I get it to print something when the checker turns out false for every item in a list? 如何让列表中的每个项目都有延迟发生? - How can i make something happen for every item in a list with a delay? 如何打印某些内容然后列出其中的内容? - How do I print something and then its list? 如何在一行代码中打印每个列表项? - How can I print each list item, in a single line of code? 如何在不重复的情况下在 For 中打印某些内容? - How can i print something in a For with no repeat? 给定一个项目,如何计算该项目在列表中的出现次数,如果不符合某些规定,则不打印该项目? - Given an item, how can I count the occurrences of said item in a list, and not print the item if it does not reach certain regulations? 如何打印某些内容,然后在同一行上调用打印功能? - How can I print something, and then call a function with a print on the same line? 使用 matplotlib,如何打印“实际尺寸”? - Using matplotlib, how can I print something “actual size”? 如何打印有很多撇号的东西? - How Can I Print Something That Has a Lot of Apostrophes? 如何在 while 循环中打印其他内容? - How can I print something else in a while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM