【发布时间】:2021-02-02 19:47:04
【问题描述】:
所以这是我在 pyhton 中编写的代码,由于某种原因它不起作用所以我希望这里的人可以帮助我找到解决方案 * 我只是一个初学者 *
def replace(phrase):
replaced = ""
for word in phrase:
if word == ("what"):
replaced = replaced + "the"
else:
replaced = replaced + word
return replaced
print(replace(input("enter a phrase: ")))
【问题讨论】:
-
你的 for 循环是
for word in phrase但是当你迭代一个字符串时,你实际上是在迭代每个字符。一种迭代单词的方法是for word in phrase.split(),它将phrase用空格分开。
标签: python python-3.x