# wk04_0928_判斷式
條件:關係運算式 或 邏輯運算式
1. 單向判斷式 (if...)
2. 雙向判斷式 (if...else)
3. 多向判斷式 (if...elif...else)
4. 巢狀判斷式
```python
sunny = input("出太陽嗎? Y or N ")
if sunny.upper() != "Y" :
print("我們乖乖在家吧")
```
出太陽嗎? Y or N n
我們乖乖在家吧
```python
password = "abcd"
usr_pwd = input("enter password, please")
if usr_pwd == password :
print("welcome")
else :
print("error password, try again")
```
enter password, please123
error password, try again
```python
score = int(input("請輸入成績:"))
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)
```
請輸入成績:75
成績等級 = C
```python
now_month = int(input("請輸入月份:"))
if now_month >=3 and now_month <=5 :
season = "spring"
elif now_month >=6 and now_month <=8 :
season = "summer"
elif now_month >=9 and now_month <=11 :
season = "autumn"
else :
season = "winter"
print("現在的季節為 :", season)
```
請輸入月份:8
現在的季節為 : summer
## 【inclass practice】
### {綜合演練}
實作5
<pre>
請設計程式判斷使用者輸入的西元年是否為閏年(平年),閏年的規則是 :
西元年若是可以被100整除,又能被400整除則是閏年。
西元年若不可以被100整除,但卻能被4整除則是閏年。
<pre/>
```python
what_year = 1900
if what_year % 100 == 0 :
if what_year % 400 == 0 :
ans = "閏年"
else :
ans = "平年"
else :
if what_year % 4 == 0 :
ans = "閏年"
else :
ans = "平年"
print(ans)
```
平年
## 【afterclass practice】
1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
2. 教學影音 lesson 7
( 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
## 【self practice】
```python
#1單向
age = input("年紀大於等於十八歲嗎? Y or N ")
if age.upper() == "Y" :
print("是")
```
年紀大於等於十八歲嗎? Y or N y
是
```python
#2單向
temperature = input("溫度大於三十度嗎? Y or N ")
if temperature.upper() == "Y" :
print("是")
```
溫度大於三十度嗎? Y or N n
```python
#3單向
name = input("請輸入名字")
if name == "Alice" :
print("是")
```
請輸入名字Alice
是
```python
#4單向
weather = input("天氣如何? sunny or rainy ")
if weather != "sunny" :
print("False")
```
天氣如何? sunny or rainy rainy
False
```python
#5單向
number = input("請輸入任意數字:")
if number == "" :
print("空")
```
請輸入任意數字:
空
```python
#1雙向
score = int(input("請輸入成績:"))
if score >= 60 :
print("及格")
else :
print("不及格")
```
請輸入成績:60
及格
```python
#2雙向
age = int(input("請輸入年紀:"))
if age >= 18 :
print("成年")
else :
print("未成年")
```
請輸入年紀:16
未成年
```python
#3雙向
weather = input("is_raining? T or F ")
if weather.upper() == "T" :
print("帶傘")
else :
print("不帶傘")
```
is_raining? T or F f
不帶傘
```python
#4雙向
day = input("請輸入星期幾:")
if day == "星期六" or "星期日" :
print("周末")
else :
print("工作日")
```
請輸入星期幾:星期日
周末
```python
#5雙向
grade = input("請輸入成績:")
if grade.upper() == "A" :
print("優等生")
elif grade.upper() == "B" :
print("良好")
else :
print("及格")
```
請輸入成績:a
優等生
```python
#1多向
season = int(input("請輸入月份:"))
if season <=2 and season ==12 :
print("winter")
elif season >=3 and season <=5 :
print("spring")
elif season >=6 and season <=8 :
print("summer")
else :
print("autumn")
```
請輸入月份:10
autumn
```python
#2多向
score = int(input("請輸入成績:"))
if score >=90 :
print("A")
elif score >=80 :
print("B")
elif score >=70 :
print("C")
elif score >=60 :
print("D")
else :
print("E")
```
請輸入成績:99
A
```python
#3多向
hour = int(input("請輸入幾點鐘(24小時制):"))
if hour >=6 and hour <=12 :
print("早安")
elif hour >=12 and hour <=18 :
print("下午好")
else :
print("晚安")
```
請輸入幾點鐘(24小時制):13
下午好
```python
#4多向
day = input("請輸入星期幾:")
if day =="星期一" :
print("開會")
elif day =="星期三" :
print("運動")
elif day =="星期五" :
print("看電影")
else :
print("自由")
```
請輸入星期幾:星期五
看電影
```python
#5多向
user_role = input("請輸入使用者角色:")
if user_role == "admin" :
print("完整權限")
elif user_role == "editor" :
print("編輯權限")
elif user_role == "guest" :
print("只讀權限")
else :
print("無權限")
```
請輸入使用者角色:bella
無權限
```python
#1巢狀
grade = int(input("請輸入成績:"))
if grade >= 60 :
if grade >= 90 :
print("A")
elif grade >= 80 :
print("B")
elif grade >= 70 :
print("C")
elif grade >= 60 :
print("D")
else :
print("F")
```
請輸入成績:60
D
```python
#2巢狀
name = input("請輸入用戶名:")
password = int(input("請輸入密碼:"))
if name == "admin" :
if password == 12345 :
print("驗證成功")
else :
print("驗證失敗")
else :
print("驗證失敗")
```
請輸入用戶名:admin
請輸入密碼:12345
驗證成功
```python
#3巢狀
age = int(input("請輸入年紀:"))
gender = input("請輸入性別:")
if age >= 18 :
if gender == "男性" :
print("可以參加男子比賽")
else :
print("不符合資格")
else :
print("不符合資格")
```
請輸入年紀:16
請輸入性別:男性
不符合資格
```python
#4巢狀
weather = input("請輸入天氣:")
temperature = int(input("請輸入溫度:"))
if weather == "晴天" :
if temperature >= 25 :
print("建議穿短袖")
else :
print("建議穿長袖")
else :
print("建議帶雨傘")
```
請輸入天氣:晴天
請輸入溫度:24
建議穿長袖
```python
#5巢狀
grade = int(input("請輸入成績:"))
attendance = int(input("請輸入出席率趴數:"))
if grade >= 90 :
if attendance >= 95 :
print("獲得全額獎學金")
elif attendance >= 90 :
print("獲得半額獎學金")
else :
print("未獲得獎學金")
elif grade >= 80 :
if attendance >= 90 :
print("獲得半額獎學金")
else :
print("未獲得獎學金")
else :
print("未獲得獎學金")
```
請輸入成績:95
請輸入出席率趴數:93
獲得半額獎學金