【发布时间】:2022-06-19 06:13:47
【问题描述】:
我的输出有问题。 我很确定这是我的打印语句或我的句子。替换编码。
这是我的代码:
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)
这里是示例输入:
automobile car manufacturer maker children kids
The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.
预期输出:
The car maker recommends car seats for kids 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 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.
【问题讨论】:
-
在所有替换完成后的 for 循环之后打印。此外,第二个 for 循环应该在第一个之后,而不是在第一个之后。
-
标签: python