[Python] 8.22 Python IT17: Replacement words

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I've been trying to figure out this Python 3 Programming task for school, and I can't get it to work:


    Write a program that replaces words in a sentence. The input begins with word replacement pairs (original and replacement). The next line of input is the sentence where any word on the original list is replaced.

    Ex: If the input is:

    automobile car manufacturer maker children kids The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.

    the output is:

    The car maker recommends car seats for kids if the car doesn't already have one.

    You can assume the original words are unique.
    ​

    Here's my current code/attempt:

    replaceList = input()
    a1 = replaceList.split()

    sentence = input()
    for i in sentence:
    i = int(i)
    output = sentence.replace(a1, a1[i+1])
    print(output)

    Continue reading...

Compartilhe esta Página