Discuss / Python / 作业三,这是不是最蠢的办法,话说r += x提取到if外,为什么答案就不对了,有能解答的吗

作业三,这是不是最蠢的办法,话说r += x提取到if外,为什么答案就不对了,有能解答的吗

Topic source

jsun_0701

#1 Created at ... [Delete] [Delete and Lock User]
def str2float(s):    def char2num(i):        digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}        return digits[i]    index = s.index('.')    length = len(s)    r = 0    for i in range(length):        if i < index:            x = char2num(s[i]) * pow(10, (- i + index - 1))            r += x        if i > index:            x = char2num(s[i]) * pow(10, index - i)            r += x    return r

  • 1

Reply