我想删除一个字符串中花括号之间的所有内容,并试图递归地执行该操作。 当递归结束时,我在这里返回x
,但不知怎的,函数doit
在这里返回None
。虽然在def内打印x
将打印正确的字符串。 我在做什么错?奇怪的函数返回值?
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))
输出:
None
我会注意这是做的一个非常糟糕的方式,但我假定这是一个编程练习,给予“我米试图做递归“声明。 –
@Lattyware是啊!我试图通过这个解决SO问题。 –