Discuss / Python / 作业

作业

Topic source

EddieLau_

#1 Created at ... [Delete] [Delete and Lock User]
from functools import reduce

def str2num(s):
    if '.' in s : 
        return float(s)
    return int(s)

def calc(exp):
    ss = exp.split('+')
    ns = map(str2num, ss)
    return reduce(lambda acc, x: acc + x, ns)

def main():
    r = calc('100 + 200 + 345')
    print('100 + 200 + 345 =', r)
    r = calc('99 + 88 + 7.6')
    print('99 + 88 + 7.6 =', r)

main()


  • 1

Reply