Discuss / Python / 第三题,写得很繁琐...学习下面大神吧

第三题,写得很繁琐...学习下面大神吧

Topic source

Gussun

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding:utf-8 -*-
from functools import reduce
def str2float(s):
    s1,s2=s.split('.')
    x=s1+s2
    def str2int(n):
        return({'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[n])
    def fn(x,y):
        return x*10+y
    return reduce(fn,map(str2int,x))/10**(len(s)-s.index('.')-1)

print(str2float(input('请输入浮点数:')))

  • 1

Reply