## wk02_0914_變數與運算式 amand change 1. 變數 2. 運算式 3. input()、output() ## [inclass practice] MBI值稱為身體質量指標,是一個簡易判斷身體胖瘦程度的方法。 計算BMI值的公式體重(單位為公斤)除以身高(單位為公尺)的平方 : BMI = 體重(KG) / 身高(m)**2 ```python height=166 weight=55 bmi=55/1.66**2 print("您的身高",height,"您的體重",weight,"您的BMI",bmi) ``` 您的身高 166 您的體重 55 您的BMI 19.959355494266223 ```python my_height=input("輸入您的身高(cm)") print(type(my_height)) my_height=int(my_height) print(type(my_height)) ``` 輸入您的身高(cm)166 <class 'str'> <class 'int'> ```python my_weight=input ("輸入您的體重(kg)") print(type(my_weight)) my_weight=int(my_weight) print(type(my_weight)) ``` 輸入您的體重(kg)55 <class 'str'> <class 'int'> ```python height=166 weight=55 bmi=55/1.66**2 #print("您的身高",height,"您的體重",weight,"您的BMI",bmi,sep="---",end="===****) print("您的身高%d ,height,您的體重%d kg,weight,您的BMI%f.1 "% (height,weight,bmi)) ``` 您的身高166 ,height,您的體重55 kg,weight,您的BMI19.959355.1 ## {範例} 格式化列印成績單 <format> 計算成績總分 <input> 計算梯形面積 <arith> 計算複利本金 <complex> ```python a=100 b=23.08 c="amanda" d=True print(type(a)) print(type(b)) print(type(c)) print(type(d)) ``` <class 'int'> <class 'float'> <class 'str'> <class 'bool'> ```python # 2.計算成績總分 <input> chinese=int(input("國文成績")) english=int(input("英文成績")) print("總分是",chinese+english) ``` 國文成績100 英文成績95 總分是 195 ```python # 3.計算梯形面積<arith> top=1 bottom=2 height=5 area=(top+bottom)*height/2 print(area) ``` 7.5 ## [afterclass practice] ### 1.綜合演練 選擇題1-10 <pre> C 1.下列何者是python的註解符號? (A)$ (B)// (C)# (D)% A 2.下列何者是錯誤變數名稱? (A)if (B)mary (C)str56 (D)error_i D 3.num=8+True,num的值為和? (A)0 (B)1 (C)8 (D)9 B 4.print(type(56.0))顯示的結果為何? (A)str (B)float (C)int (D)double A 5.下列何錯誤? (A)print(23+"67") (B)print(23+int("67")) (C)print(str(23)+"67") (D)print(str(23)+str("67")) B 6.num=96%5,則num的值為何? (A)0 (B)1 (C)19 (D)20 D 7.num=5,則num**=3 的值為何? (A)3 (B)15 (C)25 (D)125 B 8.print("78"+"12")的結果為何? (A)90 (B)7812 (C)66 (D)產生錯誤 A 9.print(78+12)的結果為何? (A)90 (B)7812 (C)66 (D)產生錯誤 A 10.下列何者運算的優先順序最高? (A)-(負) (B)* (C)and (D)+= </pre> ### 2.教學影音 lesson 4 <pre> 變數:必須是英文字母、數字或_,第一個字不能為數字 刪除變數:del age 變數資料型態:整數(int)、浮點數(float)、字串(str)、布林(bool) 查看資料型態:type() 資料型轉換:int()、float()、str() </pre> ```python #3. num=8+True print(num) ``` 9 ```python #4. print(type(56.0)) ``` <class 'float'> ```python #5.(A) print(23+"67") ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[20], line 2 1 #5.(A) ----> 2 print(23+"67") TypeError: unsupported operand type(s) for +: 'int' and 'str' ```python #5.(B) print(23+int("67")) ``` 90 ```python #5.(C) print(str(23)+"67") ``` 2367 ```python #5.(D) print(str(23)+str("67")) ``` 2367 ```python #6. num=96%5 print(num) ``` 1 ```python #7. num=5 print(num**3) ``` 125 ```python #8. print("78"+"12") ``` 7812 ```python #9. print(78+12) ``` 90 ## [self practice] 矩形的面積是長X寬、周邊長是(長+寬)X2。請設計程式讓使用者輸入矩形的長和寬,然後計算矩形的面積和周邊長。 ```python long=int(input("長")) width=int(input("寬")) print("周邊長",(long+width)*2,"面積",long*width) ``` 長5 寬6 周邊長 22 面積 30 ## [Note] ```python height=166 weight=55 bmi=55/1.66**2 print(bmi) ``` 19.959355494266223 <pre> height=166 weight=55 bmi=55/1.66**2 print(bmi) 1.**是平方的意思 2.print把內容列出來 </pre> <pre> height=166 weight=55 bmi=55/1.66**2 #print("您的身高",height,"您的體重",weight,"您的BMI",bmi,sep="---",end="===****) print("您的身高%d ,height,您的體重%d kg,weight,您的BMI%f.1 "% (height,weight,bmi)) 1.sep=,在字串中空行地方會出現的東西 2.end=,在最後時出現的字句 3./n,換行 4.%,註解 5.%.1,只有小數後一位 </pre> <pre> 2.計算成績總分 <input> chinese=int(input("國文成績")) english=int(input("英文成績")) print("總分是",chinese+english) 1.int是整數的意思 </pre>