我想使用递归采取混合字符串并在整数上添加值。一个例子输入是“ab4h5h6”和放出来会是15Python使用递归采取混合字符串,并将字符串中的整数加起来
这里是我到目前为止的代码
def toNumber(s):
total = 0
if len(s) == 0:
return 0
else:
first = s[0]
rest = s[1:]
num_rest = toNumber(s[1:])
if first.isdigit()== True:
return int(first) + total
else:
if rest.isdigit()== True:
return int(rest) + int(num_rest)
我已经尝试了很多不同的东西,但我似乎无法得到所期望的结果。
请不要在你的代码中使用== True。 – Brian