我是新来的,也是python,但我想试一试!我想在一个句子中用“top”替换“t”,用“hop”替换“h”,只有当“th”不可见时,因为“th”会变成“thop”。例如:“Thi hi tea”必须变成“thopi hopi topea”。我有这个代码:
sentence = str(raw_input('give me a sentence '))
start = 0
out = ''
while True:
i = string.find( sentence, 'th', start )
if i == -1:
sentence = sentence.replace('t', 'top')
sentence = sentence.replace('h', 'hop')
break
out = out + sentence[start:i] + 'thop'
start = i+2
但不工作......有什么想法吗?