dimanche 8 février 2015

Trouble getting if statement to work in def, but not in interpreter

When I run these commands in the interpreter I get the result I want. However when I try to run it using a .py file I do not. Im new to coding and in my brain I don't understand why this code does not work.


in the interpreter:



>>> 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'


This is the result I want.


When I run this code I do not get the result I want. What am I doing wrong, and why does this code not work?



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!')


This is a exercise from the google intro to python course. I have the correct answer from the example, and understand how that works. Im just not sure why my code is not working.


Thanks for the help.


Aucun commentaire:

Enregistrer un commentaire