代码如下
def strToInt(s):
result =0
if(len(s) == 1):
return int(s)
else:
start = int(s[0:1])
result = (result+start)*10+strToInt(s[1:])
return result
print(strToInt('781'))
运行结果不如人意:151 ,应该是781呀
百思不得其解
def change(s):
if len(s) == 1:
return int(s)
res = int(s[0]) * (10 ** (len(s)-1)) + change(s[1:])
return res
change('781')
(result+start)*10
改为
(result+start*10)
def strToInt(s):
bs = "1"
bs_list = []
res = 0
for i in range(len(s)):
t_bs = int(bs)
bs_list.append(t_bs)
bs += "0"
n = 1
for item in range(len(s)):
ad = int(s[item]) * int(bs_list[-n])
n += 1
res += ad
return res
print(strToInt('78111'))