# wk02_0914_變數和運算式 B1143024 <pre> 1. 變數 2. 運算式 3. print() input() </pre> <pre> ## [inclass practice] {綜合演練5} MBI值稱為身體質量指標,是一個簡易判斷身體胖瘦程度的方法。 計算BMI值的攻勢式體重(單位為公斤)除以身高(單位為公尺)的平方 : BMI = 體重(KG) / 身高(m)**2 請幫忙設計一個程式讓使用者輸入他的身高(公分)及體重(公斤)後計算出他的BMI值。 In [3]: x = 155 y = 50 bmi = y / (x/100)**2 print(bmi) 20.811654526534856 In [2]: weight = input("請輸入您的體重 kg") height = input("請輸入您的身高 cm") print(weight,height) weight = int(weight) height = int(height) type(weight) my_bmi = weight / (height/100)**2 print(my_bmi ) 請輸入您的體重 kg50 請輸入您的身高 cm155 50 155 20.811654526534856 In [11]: a = "B1143024" x = 155 y = 50 bmi = y / (x/100)**2 # print(x, y, bmi, end="---the end---") print("%s您的身高%d 您的體重%d BMI= %.1f" % (a,155, 50, bmi)) B1143024您的身高155 您的體重50 BMI= 20.8 </pre> <pre> ### {範例} 1. 格式化列印成績 2. 計算成績總分 3. 計算梯形面積 4. 計算複利本金 In [18]: # 計算成績總分 chinese = float(input("請輸入國文成績")) english = float(input("請輸入英文成績")) math = float(input("請輸入數學成績")) total = chinese + english + math print("國文 %d, 英文%d, 數學%.1f 總分%.1f" % (chinese,english,math,total)) 請輸入國文成績50 請輸入英文成績60 請輸入數學成績30 國文 50, 英文60, 數學30.0 總分140.0 In [20]: # 計算梯形面積 上底 = int(input("請輸入上底")) 下底 = int(input("請輸入下底")) 高 = int(input("請輸入高")) 面積 = (上底 + 下底)*高 / 2 print("上底%d, 下底%d, 高%d 面積%d" % (上底,下底,高,面積)) 請輸入上底10 請輸入下底20 請輸入高4 上底10, 下底20, 高4 面積60 </pre> <pre> [afterclass practice] 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 教學影音 lesson 4[、5、6] (A) 下列何者錯誤? (A)print(23 + "67") (B)print(23 + int("67")) (C)print(str(23) + "67") (D)print(str(23) + str("67")) In [14]: type(str(23)+'67') Out[14]: str </pre>
{"title":"wk02","description":"wk02_0914_變數和運算式 B1143024","contributors":"[{\"id\":\"4f0ea7d8-a994-4abc-a066-938bfab2315c\",\"add\":1597,\"del\":0}]"}
Expand menu