python活力練習Day14

Day14:對數字進行加減乘除四則運算express

  eg : input : 3 + 2 * {1+2 * [-4 / (8-6) + 7]}函數

         output: 25spa

#補充eval()函數code

#格式:eval(expression[,globals[,locals]])blog

#執行一個字符串表達式,並返回表達式的值,默認float類型字符串

#舉例: input

 

1 x = 7
2 print(eval("3 * x"))
3 print(eval("pow(2,2)"))

輸出結果:io

#正式進入主題class

 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())

輸出結果:float

相關文章
相關標籤/搜索