跳轉到

Selection Structures

Introduction

在我們人生中,我們會面臨很多選擇,會根據當下的情況,做出不同的決定,而程式也是一樣,我們可以根據不同的情況,執行不同的程式碼,這稱為選擇結構(Selection Structures),是流程控制的一種。

例如,我根據學測成績,來決定要填哪些志願,或是我根據天氣,來決定要不要帶傘。

if ... elif ... else

在往下之前,請你先回想 Operators - Comparison Operators 的內容,我們可以透過比較運算子來得到一個布林值,而 if 會根據 TrueFalse 來決定是否執行某段程式碼。

我們先來看一個簡單的例子:

輸入一個整數,輸出他的絕對值。

1
2
3
4
5
6
7
num = int(input("Enter a number: "))

# get the absolute value of the input
if num < 0:
    num = -num

print(num)
input
-1984
1984
ouput
1984
1984

Ivan Torrent - "1984" Lyrics Video

邏輯很簡單,如果輸入的數字小於零,那麼就將他放上負號。提醒你一下,這裡的 -not 一樣,都是一元運算子,

在這個例子中,我們只有用到 if,接著我們有請 else 登場。

請看下一個例子,輸入一個整數,判斷他是奇數還是偶數。

1
2
3
4
5
6
7
num = int(input("Enter a number: "))

# odd or even
if num % 2 == 0:
    print("Even")
else:
    print("Odd")
input
1983
-1982
ouput
Odd
Even

Timecop1983 - On the Run

這裡我們用到了 else,當 if 的條件不成立時,就會執行 else 的內容。

我們再來看更複雜的例子,輸入你的分數,輸出你的評價,因為有很多種評價,所以需要用到 elif

if 的條件不成立時,就會檢查下面 elif 的條件,如果 elif 的條件成立,就會執行 elif 的內容,如果目前的 elif 的條件不成立,就會檢查下一個 elif 的條件,如果所有的 elif 的條件都不成立,就會執行 else 的內容。

這樣講或許有點繞口,你可以看看下面的程式碼,應該就能理解了。

score = int(input("Enter your score: "))

if score >= 90:
    print("A")
    print("Excellent!")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
elif score >= 60:
    print("D")
else:
    print("F")
input
1
2
3
60
90
49
ouput
1
2
3
4
D
A
Excellent!
F

接下來給你一個可能會疑惑的例子:

score = int(input("Enter your score: "))
if score >= 60:
    print("D")
elif score >= 70:
    print("C")
elif score >= 80:
    print("B")
elif score >= 90:
    print("A")
    print("Excellent!")
else:
    print("F")
input
90
ouput
D

你可能會覺得奇怪,為什麼輸入 90 會印出 D,而不是 A,這是因為 if 的條件成立時,就會執行 if 的內容,就不會檢查 elif 的條件了。

所以,你在撰寫條件式的時候,要注意順序,以及確保每個條件是否是互斥的。

score = int(input("Enter your score: "))

if 70 > score >= 60:
    print("D")
elif 80 > score >= 70:
    print("C")
elif 90 > score >= 80:
    print("B")
elif 100 >= score >= 90:
    print("A")
    print("Excellent!")
else:
    print("F")
input
1
2
3
90
4843
55
ouput
1
2
3
4
A
Excellent!
F
F

這樣就不會有問題了,但是,當我輸入超過 100 的分數時,是會印出 F 的,你該怎麼解決呢?

Nested if

if 的內容可以是另一個 if,這種結構稱為巢狀條件式(Nested if)

舉一個例子,如果你有三個整數 a, b, c ,請你輸出最大的那個數字。

a, b, c = map(int, input().split())
max_one = None

if a > b:
    max_one = a
    if c > max_one:
        max_one = c
else:
    max_one = b
    if c > max_one:
        max_one = c

print(max_one)
input
1
2
3
10 20 30
12 12 12
-3 -4 -5
ouput
1
2
3
30
12
-3

想法很簡單,先讓 a, b 進行比較,得到較大的那個數字,再讓 cmax_one 比較,得到最大的那個數字。

這裡偷偷告訴你一個神奇妙妙工具,max(),他會找出括號內的東西中最大的那個數字。

a, b, c = map(int, input().split())
print(max(a, b, c))
input
114514 1919 810
ouput
114514

match ... case

在 Python 3.10 中,新增了 match ... case ,但遺憾的是一些古老的 Online Judge 的 Python 版本太舊不能用,但我還是想介紹給你。

我們先來看一個在 if ... elif ... else 中的例子:

month = int(input("Enter a month (1-12): "))

# which season?
if 3 <= month <= 5:
    print("Spring")
elif 6 <= month <= 8:
    print("Summer")
elif 9 <= month <= 11:
    print("Fall")
elif month == 12 or month == 1 or month == 2:
    print("Winter")
else:
    print("Invalid month")
input
1
2
3
4
5
4
7
10
1
13
ouput
1
2
3
4
5
Spring
Summer
Fall
Winter
Invalid month

再來看看 match ... case 的版本:

month = int(input("Enter a month (1-12): "))

match month:
    case 3 | 4 | 5:
        print("Spring")
    case 6 | 7 | 8:
        print("Summer")
    case 9 | 10 | 11:
        print("Autumn")
    case 12 | 1 | 2:
        print("Winter")
    case _:
        print("Invalid month")
input
1
2
3
270
11
3
ouput
1
2
3
Invalid month
Autumn
Spring

是不是很簡潔呢?如果你有學過其他像是 C, C++, Java 等語言,你可能看過 switch .. case,但是 match ... case 更強大!

關於 match ... case 我就介紹到這裡,更重要的是,你要知道 if ... elif ... else 的用法。

@EditTime : 2024-01-29 12:32

Practice

Reference code
1
2
3
4
5
i = int(input())
if i > 31:
    print("Value of more than 31")
else:
    print(1 << i)
Reference code
h1, m1 = map(int, input().split())
h2, m2 = map(int, input().split())

paid = 0
minutes = (h2 - h1) * 60 + (m2 - m1)
if minutes > 240:
    paid += ((minutes - 240) // 30) * 60
    minutes = 240
if minutes > 120:
    paid += ((minutes - 120) // 30) * 40
    minutes = 120
paid += (minutes // 30) * 30

print(paid)
Reference code
1
2
3
4
5
6
x, y = map(int, input().split())

if 0 <= x <= 100 and 0 <= y <= 100:
    print("inside")
else:
    print("outside")

Assignment

@EditTime : 2024-01-29 14:38