如何将列表用作响应? [重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我想让我的程序搜索列表中的列表中的任何关键字,我只是不知道代码。

acceptedyes_list = ["yes", "yeah", "Yes" , "Yeah"]

acceptedno_list = ["No", "no"]

if QuoteReplay_str !=(这是我需要我的程序来搜索列表的地方,如果用户输入的任何单词不在列表中,它将执行其他操作。)

python python-2.x
1个回答
1
投票

你可以尝试这样,我已经展示了两种方式。

这个问题已经被标记为重复,但我刚刚试图帮助你,因为这只是一个基于if-else逻辑的简单问题,而且经过多次尝试你似乎无法弄清楚问题。

您可以尝试在https://rextester.com/YBIMI61989在线运行解决方案。

第一路»

if QuoteReplay_str in acceptedyes_list:
    print("You accepted") 
elif QuoteReplay_str in acceptedno_list:
    print("You did not accept") 
else:
    print("You entered wrong choice") 

第二路(1线)»

您也可以使用lambda函数(只在一行中)完成此操作,如下所示:

message = (lambda s:"You accepted" if s in acceptedyes_list else "You did not accept" if s in acceptedno_list else "You entered wrong choice")(QuoteReplay_str) 
print (message) 
© www.soinside.com 2019 - 2024. All rights reserved.