Discuss / Python / 作业3

作业3

Topic source

bxhx_501

#1 Created at ... [Delete] [Delete and Lock User]

def str2float(s): def char2num(s1): digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} return digits[s1] def fn(x, y): return x10 + y L,R=s.split('.') L1=reduce(fn,map(char2num,L)) R1=reduce(fn,map(char2num,R)) length=len(R) divide=1 while length>0: divide=divide10 length=length-1 return L1+R1/divide print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')


  • 1

Reply