四則運算(用字典實作),比較c語言的switch陳述句,
輸入格式:
在一行中輸入一個數字 在一行中輸入一個四幟運算子(+,-,*,/) 在一行中輸入一個數字
輸出格式:
在一行中輸出運算結果(小數保留2位)
代碼如下:
#!/usr/bin/python # -*- coding: utf-8 -*- sf = {'+':'x+y','-':'x-y','*':'x*y','/':'x/y if y!=0 else "divided by zero"'} x = int(input()) xysf = input() y = int(input()) result = eval(sf[xysf]) if type(result) != str: print("{:.2f}".format(result)) else: print(result)
這個程式簡單,使用eval進行公式計算,
讀書和健身總有一個在路上
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/154825.html
標籤:Python
上一篇:Redis持久化和資料恢復
