【发布时间】:2015-02-09 01:26:14
【问题描述】:
当我在解释器中运行这些命令时,我得到了我想要的结果。但是,当我尝试使用 .py 文件运行它时,我没有。我是编码新手,在我的脑海中,我不明白为什么这段代码不起作用。
在解释器中:
>>> a = 'This dinner is not that bad!'
>>> n = a.find('not')
>>> b = a.find('bad')
>>> if n < b:
a.replace(a[n:], 'good')
'This dinner is good'
这就是我想要的结果。
当我运行这段代码时,我没有得到我想要的结果。我做错了什么,为什么这段代码不起作用?
def test(s):
n = s.find('not')
b = s.find('bad')
if n < b:
s.replace(s[n:], 'good')
print test('This dinner is not that bad!')
这是来自 google intro to python 课程的练习。我从这个例子中得到了正确的答案,并理解它是如何工作的。我只是不确定为什么我的代码不起作用。
感谢您的帮助。
【问题讨论】:
标签: python string if-statement function