如何使用条件循环通过列表中的项目进行迭代?
我正在尝试编写一个非常基本的代码,该代码可以将用户提供的输入和筛选通过积极响应的列表,以查看用户的响应是否属于该列表的一部分。 下面的代码迭代列表中的每个项目,但我不希望这样做,而是希望它检查列表中的所有项目并仅在输入在列表中时打印出某些内容。我该怎么做?
positive_responses = ['Good', 'Fine', 'Okay', 'Great']
user_response = input('How are you today? - ')
for each in positive_responses:
if user_response == each:
print('Glad to hear that')
elif user_response != each:
print('What can we do to help?')
I am trying to write a very basic code that can take the input given by a user and sift through a list of positive responses to see if the user's response is part of that list.
The code below iterates for each item in the list but I do not want that, instead I want it to check all items in the list and print out something only if the input is in the list. How can I do that?
positive_responses = ['Good', 'Fine', 'Okay', 'Great']
user_response = input('How are you today? - ')
for each in positive_responses:
if user_response == each:
print('Glad to hear that')
elif user_response != each:
print('What can we do to help?')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您,则可以在操作员中使用
:
prints(例如):
If I understand you correctly you can use
in
operator:Prints (for example):