from functools import reduce
def str2num(s):
try :
ss = int(s)
except ValueError:
print('the %s vaule not is int type' % s)
finally :
ss =float(s)
print('the %s value not is float type' % s)
return ss
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()
Sign in to make a reply
用户6534737704
from functools import reduce
def str2num(s):
try :
ss = int(s)
except ValueError:
print('the %s vaule not is int type' % s)
finally :
try :
ss =float(s)
except ValueError:
print('the %s value not is float type' % s)
return ss
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()