交作业 第三题
Topic source发现pow/s.index('.')不是很普遍,所以在大神的代码基础上稍作修改。
def str2float(s):
def str2num(s):
if s != '.':
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
else:
return s
def fn(x, y):
if x == '.':
return y*0.1
if y == '.':
return x
return x*10+y
return reduce(fn, map(str2num, s))
str = input("请输入:")
print('str2float(%s)=' % str, str2float(
str) / pow(10, len(str)-str.index('.')-1))
函数部分写错了。。。
def str2float(s):
def str2num(s):
if s != '.':
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
else:
return s
def fn(x, y):
if x == '.':
return y
if y == '.':
return x
return x*10+y
return reduce(fn, map(str2num, s))
str = input("请输入:")
print('str2float(%s)=' % str, str2float(
str) / pow(10, len(str)-str.index('.')-1))
- 1
尼玛取个昵称就这么难