我希望用户输入歌词的程序(这将扩展到搜索一个网站,但我目前不需要这方面的帮助)和程序将告诉我,如果输入的信息包含一个词从一个列表。在
banned_words = ["a","e","i","o","u"] #This will be filled with swear words
profanity = False
lyrics = input ("Paste in the lyrics: ")
for word in lyrics:
if word in banned_words:
print("This song says the word "+word)
profanity = True
if profanity == False:
print("This song is profanity free")
这段代码只是输出“这首歌没有亵渎”
我有几个建议:
str.split
按空格拆分。在set
进行O(1)查找。这由{}
表示,而不是用于列表的[]
。在return
。这样就不再需要else
语句。在str.casefold
捕获大小写单词。在下面是一个例子:
相关问题 更多 >
编程相关推荐