本程序通過一個while循環分兩步計算圖形面積,首先確認圖形形狀,然后根據不同的圖形采用不同的面積公式進行計算,目前可支持矩形、正方形、三角形、梯形、圓形。
感興趣的朋友還可以添加其它圖形面積計算,也可對程序退出部分進行完善。
運行截圖如下:
1 while True: 2 3 shape = input("請輸入要計算面積的圖形:(矩形,正方形,三角形,梯形,圓形)") 4 5 if (shape == "正方形") or (shape == "矩形"): 6 7 a = int(input("請輸入長:")) 8 9 b = int(input("請輸入寬:")) 10 11 print(f"圖形面積是{a}*{b}={a * b}") 12 13 elif shape == "三角形": 14 15 a = int(input("請輸入底:")) 16 17 b = int(input("請輸入高:")) 18 19 print(f"圖形面積是({a}*{b})÷2={a * b / 2}") 20 21 elif shape == "梯形": 22 23 a = int(input("請輸入上底:")) 24 25 b = int(input("請輸入下底:")) 26 27 c = int(input("請輸入高:")) 28 29 print(f"圖形面積是({a}+{b})×{c}÷2={(a + b) * c / 2}") 30 31 elif shape == "圓形": 32 33 r = int(input("請輸入半徑:")) 34 35 print(f"圖形面積是π{r}²={r ** 2 * 3.14}") 36 37 else: 38 39 print("其他圖形暫不支持,歡迎添加!")
更多Python源代碼,歡迎微信關注:Python代碼大全

