python活力练习Day14
Day14:对数字进行加减乘除四则运算
eg : input : 3 + 2 * {1+2 * [-4 / (8-6) + 7]}
output: 25
#补充eval()函数
#格式:eval(expression[,globals[,locals]])
#执行一个字符串表达式,并返回表达式的值,默认float类型
#举例:
1 x = 7 2 print(eval("3 * x")) 3 print(eval("pow(2,2)"))
输出结果:
#正式进入主题
1 def fun(): 2 list = [i for i in input()] 3 list_l = ["[","{"] 4 list_r = ["]","}"] 5 for i in range(len(list)): 6 if list[i] in list_l: 7 list[i] = "(" 8 elif list[i] in list_r: 9 list[i] = ")" 10 list = ''.join(str(i) for i in list) 11 return int(eval(list)) 12 13 if __name__ == "__main__": 14 print(fun())
输出结果: