# wk04_0928_Ch03判斷式_陳姿頴
## 課程大綱
3.1 Python 程式碼縮排
1. Python 程式碼縮排格式
- " : "及縮牌表示程式區塊
2. 絕對不要混用Tab鍵和空白鍵
- 一個tab鍵或4個空白鍵
3.2 判斷式
1. 程式流程控制
1. 判斷式
- 條件成立就執行指令
- 關係運算子(>,<,=)或邏輯運算子來判斷
- 結果為True或False
2. 迴圈
2. 單向判斷式 (if...)
3. 雙向判斷式 (if...else)
4. 多向判斷式 (if...elif...else)
5. 巢狀判斷式
## 本日課程目標:
條件:關係運算式 或 邏輯運算式
- 結果: 布林值(True or False)
1. 單向判斷式 (if...)
2. 雙向判斷式 (if...else)
3. 多向判斷式 (if...elif...else)
4. 巢狀判斷式
```python
#1 單向判斷式
sunny = input("出太陽嗎? Y or N ")
if sunny.upper() != "Y":
print("我們乖乖在家吧")
#==:等於/!=1:不等於
#.upper():全部視為大寫
```
出太陽嗎? Y or N y
```python
#2 雙向判斷式
password = "abcd"
usr_pwd = input("enter password, please")
if usr_pwd == password:
print("welcome")
else :
print("error password,try again")
```
enter password, pleaseabcd
welcome
```python
#成績等第區分範例
score = int(input("enter score, please"))
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print("成績等級 = ", grade)
```
enter score, please10
成績等級 = F
```python
#月份季節轉換範例-sol1
month = int(input("enter month, please"))
if month == 12 or month <=2:
season = "winter"
elif 3 <= month <=5:
season = "spring"
elif 6 <= month <=8:
season = "summer"
else:
season = "autumn"
print(" season = ", season)
```
enter month, please1
season = spring
```python
#月份季節轉換範例-sol2
now_month = 1
if now_month >= 3 and now_month <= 5:
season = "春"
elif now_month >= 6 and now_month <= 8:
season = "夏"
if now_month >= 9 and now_month <= 11:
season = "秋"
elif now_month == 12 or now_month == 1 or now_month == 2 :
season = "冬"
print("現在是",season)
```
現在是 冬
## [inclass practice]
### {綜合演練}
實作1
<pre>
小明出門經常忘記帶雨傘,碰到下雨就淋成落湯雞。他的好友英倫決定幫忙他設計一個檢查是否要帶傘的程式;如果今天會下雨,就提醒小明"出門記得帶傘!!",讓小明從此過著幸福快樂的日子。
</pre>
```python
rainy = input("今天會下雨嗎? Y or N ")
if rainy.upper() == "Y":
print("出門記得帶傘!!")
```
今天會下雨嗎? Y or N y
出門記得帶傘!!
實作5
請設計程式判斷使用者輸入的西元年是否為閏年(平年),閏年的規則是 :
西元年若是可以被100整除,又能被400整除則是閏年。
西元年若不可以被100整除,但卻能被4整除則是閏年。
```python
year = int(input("現在是甚麼年"))
if year % 100 == 0:
if year % 400 == 0:
a ="潤年"
else:
a = "平年"
else:
if year % 4 ==0:
a = "潤年"
else:
a = "平年"
print("現在是",a)
```
現在是甚麼年1300
現在是 平年
## [afterclass practice]
1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
##### ( A ) 1. Python 語言以下列那一個符號及縮排來表示程式區塊?
(A)「:」 (B)「!」 (C)「#」 (D)「\」
##### ( B ) 2. 「if 條件式:」的敘述中,下列那一項正確?
(A) 當條件式為 False 時,就會執行程式區塊的敘述。
(B) 當條件式為 True 時,就會執行程式區塊的敘述。
(C) 當條件式改變時,就會執行程式區塊的敘述。
(D) 當發生錯誤時,就會執行程式區塊的敘述。
##### ( C ) 3. 「if…elif…else」條件式中,如果所有條件式都是 False,則執行下列那一程式區塊?
(A) if (B) elif (C) else (D)不會執行程式區塊的敘述
##### ( C ) 4. 「if …else…」條件式的敘述中,下列那一項正確?
(A) 條件式只可使用關係運算式。
(B) 條件式只可使用邏輯運算式。
(C) 當條件可以是關係運算式,也可以是邏輯運算式。
(D) 以上皆不正確。
##### ( B ) 5. 變數 a 的值為 3,執行下列程式後顯示的結果為何?
if (a==5):
print("1",end="")
print("2",end="")
(A)1 (B) 2 (C) 12 (D) 不顯示任何內容
```python
a = 3
if (a==5):
print("1",end="")
print("2",end="")
```
2
##### ( A ) 6. 變數 a 的值為 5,執行下列程式後顯示的結果為何?
if (a==5):
print("1",end="")
else:
print("2",end="")
(A) 1 (B) 2 (C) 12 (D) 不顯示任何內容
```python
a = 5
if (a==5):
print("1",end="")
else:
print("2",end="")
```
1
##### ( 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
```python
a = 4
if (a==5):
print("1",end="")
elif(a!=4):
print("2",end="")
else:
print("3",end="")
```
3
##### ( C ) 8. 變數 a 的值為 20000,執行下列程式後顯示的結果為何?
if (a >= 10000):
if (a >= 100000):
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 元
```python
a = 20000
if (a >= 10000):
if (a >= 100000):
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")
```
18000.0 元
###### ( A ) 9. 變數 a = 3、b=7,執行下列程式後顯示的結果為何?
if (a>5 or b>5):
print(a)
else:
print(b)
(A) 3 (B) 7 (C) 37 (D) 不顯示任何內容
```python
a = 3
b = 7
if (a>5 or b>5):
print(a)
else:
print(b)
```
3
##### ( B ) 10. 變數 a = 3、b=7,執行下列程式後顯示的結果為何?
if (a>5 and b>5):
print(a)
else:
print(b)
(A) 3 (B) 7 (C) 37 (D) 不顯示任何內容
```python
a = 3
b = 7
if (a>5 and b>5):
print(a)
else:
print(b)
```
7
2. 教學影音 lesson 7
## [selfpractice]
- 單向判斷式(單一條件):
1. 判斷變數age是否大於等於18歲
2. 判斷變數temperature是否大於30度
3. 判斷字串name是否等於”Alice”
4. 判斷布林值is_sunny是否為True
5. 判斷列表numbers是否為空
```python
#1
age = int(input("年紀幾歲"))
if age >= 18:
print("已經18歲了")
```
年紀幾歲20
已經18歲了
```python
#2
temperature = float(input("現在溫度幾度"))
if temperature > 30:
print("溫度大於30度")
```
現在溫度幾度35.5
溫度大於30度
```python
#3
name = str(input("叫甚麼名字"))
if name.lower() == "alice":
print("您是Alice")
```
叫甚麼名字ALICE
您是Alice
```python
#4
is_sunny = input("晴天嗎? Y or N")
if is_sunny.upper() == "Y":
print("True")
```
晴天嗎? Y or Ny
True
```python
#5
numbers = input("輸入列表")
if not numbers:
print("列表是空的")
```
輸入列表
列表是空的
- 雙向判斷式(if-else條件):
1. 如果score大於等於60,則結果為”及格”,否則為”不及格”
2. 如果age大於等於18,則結果為為”成年”,否則為”未成年”
3. 如果is_raining為True,則結果為”帶傘”,否則為””
4. 如果day等於”星期六”或”星期日”,則結果”周末”,否則為”工作日”
5. 如果grade為”A”, 則結果”優等生”, 如果grade為”B”, 則結果為”良好”,否則為”及格”
```python
#1
score = int(input("enter your score"))
if score >= 60:
print("及格")
else:
print("不及格")
```
enter your score60
及格
```python
#2
age = int(input("enter your age"))
if age >= 18:
print("成年")
else:
print("未成年")
```
enter your age2
未成年
```python
#3
is_raining = input("下雨嗎? Y or N ")
if is_raining.upper() == "Y":
print("帶傘")
else:
print("不帶傘")
```
下雨嗎? Y or N n
不帶傘
```python
#4
day = input("今天星期幾? ")
if day == "星期六" or day == "星期日":
print("周末")
else:
print("工作日")
```
今天星期幾? 星期六
周末
```python
#5
grade = input("成績等第為多少?")
if grade.upper() == "A":
print("優等生")
elif grade.upper() == "B":
print("良好")
else:
print("及格")
```
成績等第為多少?b
良好
- 多項判斷式(if-elif-else 條件):
1. 根據變數 month 的値來判斷季節,如果是1、2、12月,結果為"冬季";如果是3、4、5月,結果為"春季";如果是6、7、8月,結果為"夏季";否則結果為"秋季"。
2. 根據 score 的分數來判斷等級,如果分數大於等於 90,結果為"A";如果分數大於等於 80,結果為"B";如果分數大於等於 70,結果為"C";如果分數大於等於 60,結果為"D";否則結果為 "F"。
3. 根據 hour 的時間來判斷問候語,如果時間在早上6點到中午 12 點之間,結果為"早安";如果時間在中午 12 點到下午6點之間,結果為"下午好";否則結果"晚安"。
4. 根據 day_of week 的星期幾來判斷行程,如果是星期一,結果為"開會";如果是星期三,結果為"運動";如果是星期五,結果為"看電影";否則結果為"自由"。
5. 根據 user_role 的使用者角色來判斷權限,如果是"admin",結果為"完整權限";如果是"editor",結果為"編輯權限";如果是 "guest",結果為"只讀權限";否則結果為"無權限"。
```python
#1
month = int(input("enter month, please"))
if month == 12 or month <=2:
season = "冬季"
elif 3 <= month <=5:
season = "春季"
elif 6 <= month <=8:
season = "夏季"
else:
season = "秋季"
print(season)
```
enter month, please1
冬季
```python
#2
score = int(input("enter score, please"))
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(grade)
```
enter score, please90
A
```python
#3
hour = int(input("現在幾點(hour)?"))
if 6 <= hour < 12:
print("早安")
elif 12 <= hour < 18:
print("下午好")
else:
print("晚安")
```
現在幾點(hour)?8
早安
```python
#4
day_of_week = input("今天星期幾?")
if day_of_week == "星期一" or day_of_week == "1":
print("開會")
elif day_of_week == "星期三" or day_of_week == "3":
print("運動")
elif day_of_week == "星期五" or day_of_week == "5":
print("看電影")
else:
print("自由")
```
今天星期幾?星期一
開會
```python
#5
user_role = input("輸入您的角色")
if user_role.lower() == "admin":
print("完整權限")
elif user_role.lower() == "editor":
print("編輯權限")
elif user_role.lower() == "guest":
print("只讀權限")
else:
print("無權限")
```
輸入您的角色0
無權限
- 巢狀判斷式
1. 根據學生成績判斷等級,如果分數大於等於90,顯示"A",如果分數在80到89之間,顯示"B",如果分數在70到79之間,顯示"C",如果分數在60到69之間,顯示"D",否則顯示"F"。
2. 根據用戶名和密碼進行身份驗證,如果用戶名是"admin"且密碼是"12345",顯示"驗證成功",否則顯示"驗證失敗"。
3. 根據年齡和性别判斷是否可以參加特定活動。如果年齡大於等於18且性別是"男性",顯示"可以參加男子比賽",否則顯示"不符合資格"。
4. 根據天氣情況和溫度判斷穿著建議。如果天氣是晴天且溫度大於25度,建議穿短袖;如果天氣是晴天但溫度不到25度,建議穿長袖;否則,建議帶雨傘。
5. 根據考試分數和出席狀況判斷學生是否獲得獎學金。如果分數大於等於90且出席率大於等於 95%,獲得全額獎學金;否則,如果分數在80到89之間且出席率大於等於 90%,獲得半額獎學金;否則,未獲得獎學金。
```python
#1
score = int(input("成績幾分"))
if score >= 90:
a = "A"
elif score >= 80:
a = "B"
elif score >= 70:
a = "C"
elif score >= 60:
a = "D"
else:
a = "F"
print(a)
```
成績幾分79
C
```python
#2
user_role = input("輸入您的角色")
pwd = input("輸入密碼")
if user_role == "admin":
if pwd == "12345":
print("驗證成功")
else:
print("驗證失敗")
```
輸入您的角色p
輸入密碼p
驗證失敗
```python
#3
age = int(input("輸入年紀"))
gender = input("輸入性別")
if age >= 18:
if gender == "男性" or gender == "男":
print("可以參加男子比賽")
else:
print("不符合資格")
```
輸入年紀10
輸入性別女
不符合資格
```python
#4
weather = input("輸入天氣")
temperature = int(input("輸入溫度"))
if weather == "晴天":
if temperature >= 25 :
print("建議穿短袖")
if temperature < 25 :
print("建議穿長袖")
else:
print("建議帶雨傘")
```
輸入天氣晴天
輸入溫度24
建議穿長袖
```python
#5
score = int(input("輸入考試成績"))
attendance = float(input("輸入出席狀況"))
if score >= 90:
if attendance >= 0.95:
print("獲得全額獎學金")
elif 80 <= score <= 89 :
if attendance >= 0.90 :
print("獲得半額獎學金")
else:
print("未獲得獎學金")
```
輸入考試成績90
輸入出席狀況0.98
獲得全額獎學金