I am trying to remove everything between curly braces in a string, and trying to do that recursivesly.
And I am returning x here when the recursion is over, but somehow the function doit is returning None here. Though printing x within the def prints the correct string.
What am I doing wrong?
strs = "i am a string but i've some {text in brackets} braces, and here are some more {i am the second one} braces"
def doit(x,ind=0):
if x.find('{',ind)!=-1 and x.find('}',ind)!=-1:
start=x.find('{',ind)
end=x.find('}',ind)
y=x[start:end+1]
x=x[:start]+x[end+1:]
#print(x)
doit(x,end+1)
else:
return x
print(doit(strs))
output:
None
You never return anything if the
ifblock succeeds. Thereturnstatement lies in theelseblock, and is only executed if everything else isn’t. You want to return the value you get from the recursion.