### wk03_0921_變數與運算式 #### 【inclass practice】 ##### {上週使用函數} 1. print() - 將指定的內容輸出到螢幕上 2. input() - 等待使用著輸入一些文本,並將輸入的文本作為字符串返回 3. type() - 獲取指定對象的類型,例如 int、float、str 等 4. int() - 將一個值轉換為整數 5. float() - 將一個值轉換為浮點數 6. str() - 將一個值轉換為字符串 ##### {綜合演練} ###### 1. 實作題3 <pre> 矩形的面積是長*寬、周邊長是(長+寬)*2 請設計程式讓使用者輸入矩形的長和寬,然後計算矩形面積和周邊長。</pre> 1. input : 矩形的長、寬 2. process 3. output : 矩形的面積、周長 ```python length = 5 width = 6 area = length * width perimeter = (length + width) *2 print("矩形的面積:", area) print("矩形的周長:", perimeter) ``` 矩形的面積: 30 矩形的周長: 22 ```python length = int(input("輸入矩形的長")) width = int(input("輸入矩形的寬")) area = length * width perimeter = (length + width) *2 print("矩形的面積:", area) print("矩形的周長:", perimeter) ``` 輸入矩形的長5 輸入矩形的寬6 矩形的面積: 30 矩形的周長: 22 ```python print("矩形的面積:" + str(area)) print("矩形的周長:" + str(perimeter)) print("矩形的面積: %.1f,周長: %s" % (area, perimeter)) print("矩形的周長:{1},面積:{0}" .format (area, perimeter)) ``` 矩形的面積:30 矩形的周長:22 矩形的面積: 30.0,周長: 22 矩形的周長:22,面積:30 ###### 2. 實作題4 <pre> 許多人出國時會有公制轉英制的困擾,以長度為例,英制和公制的長度轉換公式為: 1 inch (英吋) = 2.54 cm (公分) 請設計程式讓使用者輸入公制的身高(cm),然後計算出英制的高度是幾英呎、幾英吋 1 英呎 = 12 英吋</pre> 1. input 2. process 3. output ```python height_cm = float(input("請輸入您的身高(公分):")) inch_per_cm = 1 / 2.54 inch_total = height_cm * inch_per_cm feet = int(inch_total // 12) inches = inch_total % 12 print("身高為", feet, "英呎", inches, "英吋") ``` 請輸入您的身高(公分):185 身高為 6 英呎 0.8346456692913335 英吋 ##### {概念複習} 1. 變數命名 - 合法的 - width、area - 不合法的 - 1ans、type 2. 變數資料型態 - 數值型資料類型有哪些 - float、int、bool - 5 + 5.16 + True = ??? - 字串型資料類型有哪些 - 一對 " 或 一對 ',跳脫字元 : \ 3. " + " 運篹子 4. 輸出: " %d %s " % (參數列) 5. 輸出: " {0} {1} ".format(參數列) 6. 輸入命令 7. 運算子優先順序 ```python try1 = 5 + 5.16 + True print(try1) ``` 11.16 ##### wk03_0921_運算子先後優先順序 - 測驗 1. 計算結果:5+3*2/(7-4)=? 2. 計算結果:(10%3)** 2+4//2=? 3. 計算結果:(8+2)*(5-3)/2=? 4. 計算結果:2** 3+4** (1/2)-1=? 5. 計算結果:(10>5)or(3!=3)and(8//2<=4)=? 6. 計算結果:"Hello"+" "+"World"*2=? 7. 計算結果:(15/3)%2!=0and not(10>7)=? 8. 計算結果:(3+2)** 2/4*5%6=? 9. 計算結果:2*(4+6)-(5%3)** 2=? 10. 計算結果:len("Python")+int("3")+(5>2)=? ```python #1.計算結果:5+3*2/(7-4)=? num1 = 5+3*2/(7-4) print(num1) ``` 7.0 ```python #2.計算結果:(10%3)** 2+4//2=? num2 = (10%3)** 2+4//2 print(num2) ``` 3 ```python #3.計算結果:(8+2)*(5-3)/2=? num3 = (8+2)*(5-3)/2 print(num3) ``` 10.0 ```python #4.計算結果:2** 3+4** (1/2)-1=? num4 = 2** 3+4** (1/2)-1 print(num4) ``` 9.0 ```python #5.計算結果:(10>5)or(3!=3)and(8//2<=4)=? num5 = (10>5)or(3!=3)and(8//2<=4) print(num5) ``` True ```python #6.計算結果:"Hello"+" "+"World"*2=? num6 = "Hello"+" "+"World"* 2 print(num6) ``` Hello WorldWorld ```python #7.計算結果:(15/3)%2!=0and not(10>7)=? num7 = (15/3)%2!=0 and not(10>7) print(num7) ``` False ```python #8.計算結果:(3+2)** 2/4*5%6=? num8 = (3+2)** 2/4* 5%6 print(num8) ``` 1.25 ```python #9.計算結果:2*(4+6)-(5%3)** 2=? num9 = 2*(4+6)-(5%3)** 2 print(num9) ``` 16 ```python # 10.計算結果:len("Python")+int("3")+(5>2)=? num10 = len("Python")+int("3")+(5>2) print(num10) ``` 10 #### 【afterclass practice】 ###### 實作題1 一年一班只有 2 位同學,設計程式讓老師分別輸入 2 位同學的姓名及成績, 然後計算成績總分,最後以下圖格式列印。 ```python name1 = input("請輸入第一位學生的姓名:") score1 = int(input("請輸入第一位學生的成績: ")) name2 = input("請輸入第二位學生的姓名:") score2 = int(input("請輸入第二位學生的成績: ")) print() print("姓名 成績") print("%-4s %3d" % (name1, score1)) print("%-4s %3d" % (name2, score2)) print("成績總分為:" + str(score1 + score2)) ``` 請輸入第一位學生的姓名:林大毛 請輸入第一位學生的成績: 78 請輸入第二位學生的姓名:陳學文 請輸入第二位學生的成績: 100 姓名 成績 林大毛 78 陳學文 100 成績總分為:178 ###### 實作題2 計程車計費方式第1公里為85元,多1公里加收20元。設計程式讓運將輸入乘客的搭乘公里數,然後計算程車費用。 ```python km = int(input("請輸入路程公里數 (整數):")) money = 85 + (km - 1) * 20 print("你的車程車資費為:" + str(money)) ``` 請輸入路程公里數 (整數):8 你的車程車資費為:225