### wk04_0928_判斷式
條件: 關係運算式 或 邏輯運算式
1. 單向判斷式 (if...)
2. 雙向判斷式 (if...else)
3. 多向判斷式 (if...elif...else)
4. 巢狀判斷式
#### 【inclass practice】
##### {綜合演練}
```python
sunny = input("今天有出太陽嗎? Y or N ")
if sunny == "Y" :
print("我們去玩吧!")
```
今天有出太陽嗎? Y or N Y
我們去玩吧!
```python
sunny = input("今天有出太陽嗎? Y or N ")
if sunny != "Y" :
print("我們乖乖在家吧")
```
今天有出太陽嗎? Y or N y
我們乖乖在家吧
```python
sunny = input("今天有出太陽嗎? Y or N ")
if sunny.upper() == "Y" :
print("我們去玩吧!")
```
今天有出太陽嗎? Y or N y
我們去玩吧!
```python
score = int(input("enter your score"))
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 your score80
成績等級 = B
```python
now_month = int(input("enter the month "))
if now_month >= 3 and now_month <= 5 :
season = "春"
elif now_month >= 6 and now_month <= 8 :
season = "夏"
elif now_month >= 9 and now_month <= 11 :
season = "秋"
elif now_month == 12 or now_month == 1 or now_month ==2 :
season = "冬"
print("現在是", season)
```
enter the month 11
現在是 秋
```python
password = "123"
usr_pwd = input("enter password, please")
if usr_pwd == password :
print("Welcome")
else :
print("Error Password, Please Try Again")
```
enter password, please123
Welcome
###### 實作1
<pre>
小明出門經常忘記帶雨傘,碰到下雨就淋成落湯雞。他的好友英倫決定幫忙他設計一個檢查是否要帶傘的程式;
如果今天會下雨,就提醒小明"出門記得帶傘!!",讓小明從此過著幸福快樂的日子。</pre>
```python
rain = input("今天會下雨嗎?")
if(rain=="Y" or rain=="y"):
print("出門記得帶傘!")
```
今天會下雨嗎?y
出門記得帶傘!
###### 實作5
<pre>
請設計程式判斷使用者輸入的西元年是否為閏年(平年),閏年的規則是 :
西元年若是可以被100整除,又能被400整除則是閏年。
西元年若不可以被100整除,但卻能被4整除則是閏年。</pre>
```python
what_year = int(input("今年幾年"))
if what_year % 100 == 0 :
if what_year % 400 == 0 :
ans = "閏年"
else :
ans = "平年"
else :
if what_year % 4 == 0 :
ans = "閏年"
else :
ans = "平年"
print("今年是 ", ans)
```
今年幾年2023
今年是 平年
#### 【afterclass practice】
1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
2. 教學影音 lesson 7
1. Python 語言以下列那一個符號及縮排來表示程式區塊?
(A)「:」 (B)「!」 (C)「#」 (D)「\」
Ans:(A)
2. 「if 條件式:」的敘述中,下列那一項正確?
(A) 當條件式為 False 時,就會執行程式區塊的敘述。
(B) 當條件式為 True 時,就會執行程式區塊的敘述。
(C) 當條件式改變時,就會執行程式區塊的敘述。
(D) 當發生錯誤時,就會執行程式區塊的敘述。
Ans:(B)
3. 「if…elif…else」條件式中,如果所有條件式都是 False,則執行下列那一程式區塊?
(A) if (B) elif (C) else (D)不會執行程式區塊的敘述
Ans:(C)
4. 「if …else…」條件式的敘述中,下列那一項正確?
(A) 條件式只可使用關係運算式。
(B) 條件式只可使用邏輯運算式。
(C) 當條件可以是關係運算式,也可以是邏輯運算式。
(D) 以上皆不正確。
Ans:(C)
5. 變數 a 的值為 3,執行下列程式後顯示的結果為何?
if (a==5):
print("1",end="")
print("2",end="")
(A)1 (B) 2 (C) 12 (D) 不顯示任何內容
Ans:(B)
```python
a = 3
if (a==5):
print("1",end="")
print("2",end="")
```
2
6. 變數 a 的值為 5,執行下列程式後顯示的結果為何?
if (a==5):
print("1",end="")
else:
print("2",end="")
(A) 1 (B) 2 (C) 12 (D) 不顯示任何內容
Ans:(1)
```python
a = 5
if (a==5):
print("1",end="")
else:
print("2",end="")
```
1
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
Ans:(C)
```python
a = 4
if (a==5):
print("1",end="")
elif (a!=4):
print("2",end="")
else:
print("3",end="")
```
3
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 元
Ans:(C)
```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 元
9. 變數 a = 3、b=7,執行下列程式後顯示的結果為何?
if (a>5 or b>5):
print(a)
else:
print(b)
(A) 3 (B) 7 (C) 37 (D) 不顯示任何內容
Ans:(A)
```python
a = 3
b = 7
if (a>5 or b>5):
print(a)
else:
print(b)
```
3
10. 變數 a = 3、b=7,執行下列程式後顯示的結果為何?
if (a>5 and b>5):
print(a)
else:
print(b)
(A) 3 (B) 7 (C) 37 (D) 不顯示任何內容
Ans:(B)
```python
a = 3
b = 7
if (a>5 and b>5):
print(a)
else:
print(b)
```
7