-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 に答える