spam = {"make a lot of money","buy now","subscribe this","click this"}
text = input("Enter your text: ")
if (text in spam):
print("It is spam")
else:
print("it is not spam")
此代码片段不适用于输入的
ex-text="赚很多钱",输出-";这是垃圾邮件
但是如果text="点击这个可以赚很多钱";,输出-";它不是垃圾邮件";
使用set
方法的可能解释和解决方案是什么?
你可以这样做,我认为你的主要问题是你没有检查集合中的每个项目,所以除非文本完全匹配,否则你不会得到正确的答案
spam = {"make a lot of money","buy now","subscribe this","click this"}
text = input("Enter your text: ")
if any([x in text for x in spam]):
print("It is spam")
else:
print("it is not spam")
使用any
可以将集合中的每个项目与输入进行比较,以判断其中是否至少有一个匹配。