-1

如何使用loop. 例如,假设我有一个function名为“ changeWords”,我希望这个函数将三个单词can'tshould notdon't更改为can'tshould notdo not。所以当输入函数时,'changeWords("I don't know how to do this")'应该返回'I do not know how to do this".

澄清:

changeWords(“I can't eat") -> “I can not eat"
changeWords(“I don't like swimming.”) -> “I do not like swimming.”
changeWords(“I shouldn't do that.”) -> “I should not do that.”

我的尝试:

def stringChange(a):
a = ""
for line in stringChange("a"):
    line = text.replace("a","can't","can not")
    if not line: break
    return line
4

1 回答 1

4
>>> def changeWords(s):
        for old, new in (
                ("can't", "can not"),
                ("shouldn't", "should not"),
                ("don't", "do not"),
            ):    
            s = s.replace(old, new)
        return s

>>> changeWords("I don't know how to do this")
'I do not know how to do this'
于 2012-09-13T05:27:23.873 回答