我正在开发一个程序,它接受用户输入并用'x'替换列表中的单词。 例如,这个词很糟糕,用户输入是“这个词很糟糕”。输出应该是“这个词是xxxxx。
这是我到目前为止所拥有的。如何访问列表中的元素并与用户输入匹配?
def main():
message = []
words = ['drat','crap','sucks']
counter = 0
userInput = str(input("Enter The Sentense: "))
truncatedInput = userInput[:140]
sentence = truncatedInput.split()
for i in range(len(sentence)):
答案 0 :(得分:0)
def main():
final_message = []
words = ['drat','crap','sucks']
counter = 0
userInput = input("Enter The Sentense: ") # use raw_input if you're using python2.X
truncatedInput = userInput[:140]
sentence = truncatedInput.split()
for word in sentence:
if word in words:
word = 'x' * len(word)
final_message.append(word)
print ' '.join(final_message)