2022年1月12日 星期三

Python 練習

Python 練習

1.梯形面積 計算
2.球體積  計算
3.攝氏溫度轉華氏  計算
4.坪數轉平方公尺  計算
5. BMI 計算


 
import math
#===============================
def trapezoid_1():
    while True:
        print ('1.梯形面積 計算')
        # input獲取的資料均為 字串
        a = input('請輸入梯形的上底長度:')
        # 判斷如果輸入的資料不是數位的時候跳出,如果是就繼續
        if not a.isdigit():
            continue
        b = input('請輸入梯形的下底長度:')
        if not b.isdigit():
            continue
        h = input('請輸入梯形的高:')
        if not h.isdigit():
            continue
    
        s = (float(a) + float(b)) * float(h) / 2
        print("梯形的面積為:%.2f" %s , end='')
        print('平方單位\n\n')
        # 這裡需要將str 轉換成 float
        break 
#===============================
def spherical_2() :
    while True:
        print ('2.球體積  計算')
        r=float(input("請輸入半徑:"))
        circumference=2*math.pi*r
        area=math.pi*r*r
        sarea=4*math.pi*r*r
        volume=4/3*math.pi*r**3
        print ( "圓的半徑: %.2f" % r)
        print ( "圓的周長: %.2f" % circumference)
        print ( "圓的面積: %.2f"% area, end='')
        print('平方單位') 
        print ( "球的表面積: %.2f"% sarea , end='')
        print('平方單位')
        print ( "球的體積: %.2f" % volume , end='')
        print('立方單位\n\n')
        
        break
#===============================
def Celsius_3() :
    while True:
        # 攝氏轉華氏 c2f.py
        print ('3.攝氏溫度轉華氏  計算')
        degree_c = int(input("請輸入攝氏溫度:"))
        degree_f = degree_c * 1.8 +32 
        print ("攝氏 %d 度等於華氏 %.2f 度\n\n" % (degree_c,degree_f))
        break
#===============================
def Pingnumber_4() :
    while True:
        # 1 坪=3.3058 平方公尺
        print ('4.坪數轉平方公尺  計算')
        ping = float(input("請輸入坪數 :"))
        squareM = ping*3.33058 
        print ("%.2f 坪數等於 %.2f 成平方公尺\n\n" % (ping,squareM))
        break
#===============================   
def BMI_5() :
    while True:
        print ('5. BMI 計算')
        h = float(input('請輸入身高(cm):'))/100
        # 使用 float 轉換成浮點數後除以 100 ( 因為身高可能會有小數點 )
        w = float(input('請輸入體重(kg):'))
        # 使用 float 轉換成浮點數 ( 因為體重可能會有小數點 )
    
        bmi = w/(h*h)                           # 套用公式計算
    
        print ("身高為 %.2f  (M)" % (h))
        print ("體重為 %.2f  (kg)" % (w))  
        print('你的 BMI 數值為:%.2f  (kg)\n\n' % (bmi) )         # 你的 BMI 數值為: 
        break
#===============================
# 主程式
#===============================
while True:
    print ('1.梯形面積 計算')
    print ('2.球體積  計算')
    print ('3.攝氏溫度轉華氏  計算')
    print ('4.坪數轉平方公尺  計算')
    print ('5. BMI 計算')
    print ('==================')
    in1 = input('請輸入1至5:')
    if not in1.isdigit():
        continue
        
    if  int(in1)==1:
        trapezoid_1()
    elif  int(in1)==2:
        spherical_2()
    elif  int(in1)==3:
        Celsius_3()
    elif  int(in1)==4:
        Pingnumber_4()
    elif  int(in1)==5:
        BMI_5()       
    else :
        print('請重新輸入1至5')
    continue


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...