Discuss / Python / 交作业,有没有大神写出了更优化的代码唔

交作业,有没有大神写出了更优化的代码唔

Topic source

惟莉V雅爱

#1 Created at ... [Delete] [Delete and Lock User]

def str2num(s): try: return int(s) except ValueError as v: print("Error:" , v) return float(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