from functools import reduce
def str2float(s):
def fn(x,y):
return x*10 + y
n = s.index('.') #索取小数点的索引值
L1 = list(map(int, s[:n])) #小数点前的数字转换成整型
L2 = list(map(int, s[n+1:])) #小数点后的数字转换成整型
return reduce(fn, L1) + reduce(fn,L2)/10**len(L2) #小数点后的运算要注意分母10**len(L2)
str2float('123.456')
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
print('测试成功!')
else:
print('测试失败!')