-1

我正在开发一个程序,它接受用户输入并用“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)):
4

1 回答 1

1
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)
于 2013-07-15T01:17:55.553 回答