{%hackmd @themes/orangeheart %} ## wk03_0921_變數與運算式 ### 醫放三 B1003210 應雨岑 1. print() - 顯示出print內的內容 2. input() - 顯示出對話框讓使用者輸入內容 3. type() - 顯示該函數的資料型態(類型),如:int,float,str,bool 4. int() - 讓括弧內的東西變成整數的資料型態 5. float() - 讓括弧內的東西變成含小數點的數字的資料型態 6. str() - 讓括弧內的東西變成字串、文字的資料型態 ## 今日課程內容 1. 變數 2. 資料型態 3. 輸入與輸出 4. 運算式 ## NOTE <pre> **次方 //整除的數 /除以 %餘數 and 放在兩個運算子中間,判斷True or False,當兩個都為True才會是True or 放在兩個運算子中間,判斷True or False,當兩個都為False才會是False </pre> ## [ INCLASS PRACTICE ] * ### 綜合演練 1. 實作題3 <pre> 矩形的面積是長*寬、周邊長是(長+寬)*2 請設計程式讓使用者輸入矩形的長和寬,然後計算矩形面積和周邊長。 - Step1. input 矩形的長與寬 - Step2. process - Step3. output 矩形的面積與周長 </pre> ```python length = int(input("請輸入矩形的長度")) width = int(input("請輸入矩形的寬度")) perimeter=(length+width)*2 area=length*width print("矩形的面積為%f,周長為%f"%(area,perimeter)) #%d為顯示整數,%f為顯示全部小數點float,%s當成字串顯示 #其他顯示方式 print("矩形的面積為"+str(area),"矩形的周長為"+str(perimeter)) print("矩形的面積為{0}矩形的周長為{1}".format(area,perimeter)) ``` 請輸入矩形的長度5 請輸入矩形的寬度6 矩形的面積為30.000000,周長為22.000000 矩形的面積為30 矩形的周長為22 矩形的面積為30矩形的周長為22 2. 實作題4 <pre> 許多人出國時會有公制轉英制的困擾,以長度為例,英制和公制的長度轉換公式為: 1 inch (英吋) = 2.54 cm (公分) 請設計程式讓使用者輸入公制的身高(cm),然後計算出英制的高度是幾英呎、幾英吋 1 英呎 = 12 英吋 - Step1. input 輸入身高 - Step2. process - Step3. output 輸出他身高為多少英呎英吋 </pre> ```python height= int(input("請輸入身高為多少公分(不含小數點)")) #inch (英吋) = 2.54 cm (公分) #1 英呎 = 12 英吋 inch=height/2.54 #公分轉英寸 feet=int(inch/12) #英吋換成英呎 inch=inch%12 #剩下的英寸 print("他的身高為%d英呎%d英吋"%(feet,inch)) ``` 請輸入身高為多少公分(不含小數點)167 他的身高為5英呎5英吋 ```python ##概念練習 1. 變數命名 - 合法的: width、area(只要不是系統內的字的其他英文字就可以) - 不合法的: 3_year_ago、type (若第一個字為數字或是用到python內部預定的字時,就是不可以接受的) 2. 資料型態 - 數值型的資料型態有哪些(int整數、float含小數點、bool布林) - ex:5+5.16+True = ???(11.16) - 數值型的資料型態有什麼(str表字串) ``` ```python # 課堂測驗作答 print(5+3*2/(7-4)) print((10%3)**2+4//2) print((8+2)*(5-3)/2) print(2**3+4**(1/2)-1) print((10>5)or(3!=3)and(8//2<=4)) print("Hello"+""+"World"*2) print((15/3)%2!=0 and not (10>7)) print((3+2)**2/4*5%6) print(2*(4+6)-(5%3)**2) print(len("Python")+int("3")+(5>2)) ``` 7.0 3 10.0 9.0 True HelloWorldWorld False 1.25 16 10 #### 測驗答案 ![測驗答案](https://hackmd.io/_uploads/S1txukcUT.jpg) ## note <pre> 計算符號之間可以加空白鍵 <br> ** : 幾次方<br> sep="." : 分隔符號為. end="/" : 結束用/結束 在print內寫入%d : 放入參數答案 (最後在答案要加%) %s : 放入的答案為字串 %f : 放入的答案包含全部小數點 %.1f : 包含 1位小數點 %.3f : 包含 3位小數點 寫法如下- print("身高為%d cm,體重為%d kg,BMI為%.1f " % (height,weight,bmi)) print("顯示的文字",參數名字)<br> my_height = input("請輸入身高(cm)") : 可以輸入文字進去對話框 使用者輸入的文字要轉換成"int" ()<br> print("姓名 成績") print("%3s %3d"%(name1,score1)) #按照字元數進行規則排列出一格表格格式 </pre> ## [ afterclass practice ] > 1. 綜合演練 實作題1-2 (需抄題在markdown cell) > 2. 教學影音 lesson 5[、6、1] * ## 綜合演練二 1. 老師分別輸入2位學生的姓名和成績,然後計算總分,最後以此格式顯示 ```python name1= str(input("請輸入第一位學生姓名")) score1= int(input("請輸入第一位學生成績")) name2= str(input("請輸入第二位學生姓名")) score2= int(input("請輸入第二位學生成績")) print("姓名 成績") print("%3s %3d"%(name1,score1)) #按照字元數進行規則排列出一格表格格式 print("%3s %3d"%(name2,score2)) ``` 請輸入第一位學生姓名林大毛 請輸入第一位學生成績78 請輸入第二位學生姓名陳學文 請輸入第二位學生成績100 姓名 成績 林大毛 78 陳學文 100 * ## 綜合演練二 2. 計程車計費為第1公里70元,之後多1公里多30元 ```python distance= input("請輸入:搭了幾公里(整數)") distance=int(distance) fare= 70+(distance-1)*30 print("總車費為", fare) ``` 請輸入:搭了幾公里(整數)5 總車費為 190 * ## 教學影音 lesson 5 這部影片說明很多print的用法,另外還講到print命令參數格式化format的使用方式,雖然老師在上課時有提過,不過藉由再觀看一次影片讓我更了解它的意義,影片最後也有提到input命令的內容。 ## [ self practice ] ```python year1=int(input("請輸入年度")) get1=float(input("請輸入該年所得稅")) business1=float(input("請輸入該年營業稅")) tax1=float(input("請輸入該年證交稅")) year2=int(input("請輸入年度")) get2=float(input("請輸入該年所得稅")) business2=float(input("請輸入該年營業稅")) tax2=float(input("請輸入該年證交稅")) year3=int(input("請輸入年度")) get3=float(input("請輸入該年所得稅")) business3=float(input("請輸入該年營業稅")) tax3=float(input("請輸入該年證交稅")) print("年度 所得稅 營業稅 證交稅") print("%4d %5.2f %5.2f %5.2f" % (year1, get1, business1, tax1)) #因為稅含有兩個小數點又到百分位,因此佔的字元數為5個,因此為5.2f print("%4d %5.2f %5.2f %5.2f" % (year2, get2, business2, tax2)) print("%4d %5.2f %5.2f %5.2f" % (year3, get3, business3, tax3)) ``` 請輸入年度2017 請輸入該年所得稅98.34 請輸入該年營業稅90.20 請輸入該年證交稅104.79 請輸入年度2016 請輸入該年所得稅83.00 請輸入該年營業稅110.50 請輸入該年證交稅82.45 請輸入年度2015 請輸入該年所得稅98.00 請輸入該年營業稅79.32 請輸入該年證交稅102.00 年度 所得稅 營業稅 證交稅 2017 98.34 90.20 104.79 2016 83.00 110.50 82.45 2015 98.00 79.32 102.00