### wk04_0928_B1003217 ### [inclass practice] <pre> 1.單向判斷式 (if...) 2.雙向判斷式 (if...else) 3.多向判斷式 (if...elif...else) 4.巢狀判斷式 </pre> ```python sunny=input("出太陽嗎? Y or NO ") if sunny.upper() !="Y": print("我們去玩吧") ``` 出太陽嗎? Y or NO n 我們去玩吧 <pre> 判斷式:if..else...else 迴圈:for、while </pre> ```python password="abcd" usr_pwd=input("enter password,please") if usr_pwd==password : print("weclome") else: print("error password,try again") ``` enter password,pleaseabcd weclome ```python score=int(input("請輸入你的成績")) if score >=90: grade="A" if score >=80: grade="B" if score >=70: grade="C" if score >=60: grade="D" else: grade="F" print("成績等級=",grade) ``` 請輸入你的成績52 成績等級= F ```python now_month=int(input("請輸入月份")) if now_month==12 or now_month==1 or now_month==2: season="冬季" elif now_month>=3 and now_month<=5: season="春季" elif now_month>=6 and now_month<=8: season="夏季" else: grade="秋季" print("季節為",season) ``` 請輸入月份5 季節為 春季 #### 實作5 <pre> 請設計程式判斷使用者輸入的西元年是否為閏年(平年),閏年的規則是 : 西元年若是可以被100整除,又能被400整除則是閏年。 西元年若不可以被100整除,但卻能被4整除則是閏年。 </pre> ```python now_year=int(input("請輸入年份")) if now_year%100 ==0: if now_year%400==0: ans="閏年" else: ans="平年" else: if now_year%4==0: ans="閏年" else: ans="平年" print(ans) ``` 請輸入年份2000 閏年 ### [Afterclass practice] ### 綜合演練 <pre> A 1.Python語言與下列哪一個符號及縮排來表示程式區塊? (A)「:」 (B)「!」 (C)「#」 (D)「\」 </pre> <pre> B 1.「if條件式:」的敘述中,下列哪一項正確? (A)當條件為Flase時,就會執行程式區塊的敘述 (B)當條件為True時,就會執行程式區塊的敘述 (C)當條件式改變時,就會執行程式區塊的敘述 (D)當發生錯誤時,就會執行程式區塊的敘述 </pre> <pre> C 3.「if...elif...else...」條列式中,如果所有條件都式False,則執行下列哪一程式區塊? (A)if (B)elif (C)else (D)不會執行程式區塊的敘述 </pre> ```python number=int(input("請輸入數字")) if number>=1 and number<=3: output="小" elif number>=4 and number<=6: output="中" else: output="大" print(output) ``` 請輸入數字8 大 <pre> C 4.「if...else...」條列式的敘述中,下列哪一項正確? (A)條件式只可使用關係運算式 (B)條件式只可使用邏輯運算式 (C)當條件可以是關係運算式,也可以是邏輯運算式 (D)以上皆不正確 </pre> <pre> B 5.變數a的值為3,執行下列程式後顯示的結果為何? if (a==5): print("1",end="") print("2",end="") (A)1 (B)2 (C)12 (D)不顯示任何內容 </pre> ```python a=int(input()) if (a==5): print("1",end="") print("2",end="") ``` 3 2 <pre> C 6.變數a的值為5,執行下列程式後顯示的結果為何? if (a==5): print("1",end="") print("2",end="") (A)1 (B)2 (C)12 (D)不顯示任何內容 </pre> ```python a=int(input()) if (a==5): print("1",end="") print("2",end="") ``` 5 12 <pre> C 7.變數a的值為4,執行下列程式後顯示的結果為何? if (a==5):print("1",end="") elif (a!=4): print("2",end="") else:print("3",end="") (A)1 (B)2 (C)3 (D)123 </pre> ```python a=int(input()) if (a==5):print("1",end="") elif (a!=4): print("2",end="") else:print("3",end="") ``` 4 3 <pre> C 8.變數a的值為20000,執行下列程式後顯示的結果為何? if (a>=10000): if (a>=10000): print(a*0.5,end="元\n") elif (a>=50000): print(a*0.8,end="元\n") else: print(a*0.9,end="元\n") else: print(a,end="元\n") (A)10000.0元 (B)16000.0元 (C)18000.0元 (D)20000.0元 </pre> ```python a=int(input()) if (a>=10000): if (a>=100000): print(a*0.5,end="元\n") elif (a>=500000): print(a*0.8,end="元\n") else: print(a*0.9,end="元\n") else: print(a,end="元\n") ``` 20000 18000.0元 <pre> A 9.變數a=3、b=7,執行下列程式後顯示的結果為何? if(a>5 or b>5): print(a) else: print(b) (A)3元 (B)7元 (C)37元 (D)不顯示任何內容 </pre> ```python a=int(input("a=")) b=int(input("b=")) if(a>5 or b>5): print(a) else: print(b) ``` a=3 b=7 3 <pre> B 10.變數a=3、b=7,執行下列程式後顯示的結果為何? if(a>5 and b>5): print(a) else: print(b) (A)3元 (B)7元 (C)37元 (D)不顯示任何內容 </pre> ```python a=int(input("a=")) b=int(input("b=")) if(a>5 and b>5): print(a) else: print(b) ``` a=3 b=7 7 ### 教學影音 lesson 7 <pre> 單向判斷式 if條件式: 程式區塊 雙向判斷式 if條件式: 程式區塊一 else: 程式區塊二 多向判斷式 if條件式一: 程式區塊一 elif條件式二: 程式區塊二 elif條件式三: ... else: else程式區塊 巢狀判斷式 </pre> ### [self practice] 1.判斷age是否大於等於18歲 ```python age=int(input("請輸入你的年齡")) if age >=18: print("大於等於18歲") ``` 請輸入你的年齡18 大於等於18歲 2.如果age大於等於18歲,則結果為"及格",否則為"不及格" ```python age=int(input("請輸入你的年齡")) if age >=18: print("成年") else: print("未成年") ``` 請輸入你的年齡2 未成年