Python : replacing words while iterating -
i'm not sure why code not working. trying iterate on copy of list of words , replace words given word. instead getting raised invalid syntax errors. understand (from reading other posts on here) modifying lists while iterating bad practise, therefore created copy using [:]
. here code have:
def change(z): words = z.split() in words[:]: if 'because' in i: words.replace(i, 'as') print(words) change(input("line: "))
and error:
traceback (most recent call last): file "c:/users/jarrod/desktop/py/ncss2016adv/kindlenook.py", line 9, in <module> change(input("line: ")) file "<string>", line 1 ^ syntaxerror: invalid syntax
any clues why syntax incorrect appreciated.
you should give code creates error get, stated other people in comments.
anyway, in code, i
string, not list, because iterating list split
loop. so, maybe should use if == 'because'
rather if 'because' in i
?
Comments
Post a Comment