zyBooks Lab Activity 6.19 – Replacement Words

Question:

Having trouble with my output.
I’m fairly certain it’s either my print statement or my sentence.replace coding.

Here’s my code:

word_pairs = {}

tokens = input().split()
sentence = input()

step = 2
for index in range(0,len(tokens), step):
    key = tokens[index]
    value = tokens[index+1]
    
    word_pairs[key] = value
    for original, new in word_pairs.items():
        sentence = sentence.replace(original, new)
        
        print(sentence)

Here is the sample input:

automobile car   manufacturer maker   children kids

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

Expected output:

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

My output:

The car manufacturer recommends car seats for children if the car doesn't already have one.
The car manufacturer recommends car seats for children if the car doesn't already have one.
The car maker recommends car seats for children if the car doesn't already have one.
The car maker recommends car seats for children if the car doesn't already have one.
The car maker recommends car seats for children if the car doesn't already have one.
The car maker recommends car seats for kids if the car doesn't already have one.
Asked By: Ca1amity_Jane

||

Answers:

Sorry, I didn’t have time and just put the main ideas.

Explanation:

  • I used the list slice "list[start:stop:step]" to get only the words to replace.
  • Then I used a for loop and the replace method to replace the words ["automobile", "manufacturer", "children"] with the tokens list with index 1, 3 and 5.
    ["car", "maker", "kids"]
tokens = input().split()
sentence = input()
c = 1
for word in tokens[::2]:
  sentence.replace(word, tokens[c]) 
  c += 2

print(sentence)
"The car maker recommends car seats for kids if the car doesn't already have one."

or if you want use your solution, just adjust the indentation for print

word_pairs = {}

tokens = input().split()
sentence = input()

step = 2
for index in range(0,len(tokens), step):
    key = tokens[index]
    value = tokens[index+1]
    
    word_pairs[key] = value
    for original, new in word_pairs.items():
        sentence = sentence.replace(original, new)
        
 print(sentence)
Answered By: RafaelMafra

Just put your print function outside the loop

for index in range(0,len(tokens), step):
key = tokens[index]
value = tokens[index+1]

word_pairs[key] = value
for original, new in word_pairs.items():
    sentence = sentence.replace(original, new)
    
print(sentence)
Answered By: Majed Jaber

Here is an alternative approach that makes use of the enumerate() feature:

# words = ['automobile', 'car', 'manufacturer', 'maker', 'children', 'kids']
words = input().split()
# sentence = "The automobile manufacturer recommends...have one"
sentence = input()

for index, value in enumerate(words):
    # If the index is any even number to include 0:
    if index % 2 == 0:
        """ Replaces every even index value in words that is in sentence with
            the odd index value to the right of it. Example: "automobile" is 
            replaced by "car" then "manufacturer" by "maker", etc. """ 
        sentence = sentence.replace(value, words[index + 1])

print(sentence)

Usually, you try to avoid using literals within your code. However, for indexing purposes and the nature of this lab, this approach suffices and can maintain its accuracy no matter the number of word pairs you input.

Answered By: John B.
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.