學習軌道
/
Python
Python
/
練習
/
熔毀防制
熔毀防制

熔毀防制

學習練習

簡介

在 Python 中,if、elif(是 'else and if' 的縮寫)和 else 敘述用來控制執行流程,讓程式做出判斷。 和許多其他程式語言不同,Python 3.9 及更早的版本沒有提供正式的 case-switch 敘述,而是改用多個 elif 敘述來達到類似的目的。

Python 3.10 導入了另一種 case-switch 敘述,稱為 structural pattern matching,我們會在另一個概念中另行介紹。

條件式使用的運算式必須求得 True 或 False,不論是直接回傳 bool 型別,或是評估為「truthy」或「falsy」。

x = 10
y = 5

# The comparison '>' returns the bool 'True',
# so the statement is printed.
if x > y:
    print("x is greater than y")
...
>>> x is greater than y

if 可以搭配一個非必要的 else 程式區塊,當原本的 if 條件評估為 False 時,就會執行這個區塊:

x = 5
y = 10

# The comparison '>' here returns the bool 'False',
# so the 'else' block is executed instead of the 'if' block.
if x > y:
    print("x is greater than y")
else:
    print("y is greater than x")
...
>>> y is greater than x

elif 則可以進行多次評估與分支。

x = 5
y = 10
z = 20

# The 'elif' statement allows for the checking of more conditions.
if x > y:
    print("x is greater than y and z")
elif y > z:
    print("y is greater than x and z")
else:
    print("z is greater than x and y")
...
>>> z is greater than x and y

布林運算和比較可以搭配條件式,做出更複雜的判斷:

>>> def classic_fizzbuzz(number):
        if number % 3 == 0 and number % 5 == 0:
            say = 'FizzBuzz!'
        elif number % 5 == 0:
            say = 'Buzz!'
        elif number % 3 == 0:
            say = 'Fizz!'
        else:
            say = str(number)
        
        return say

>>> classic_fizzbuzz(15)
'FizzBuzz!'

>>> classic_fizzbuzz(13)
'13'

說明

在這個練習中,我們要為核子反應爐開發一套簡單的控制系統。

反應爐要產生電力,就必須處於_臨界_狀態。 如果反應爐的狀態低於臨界,就可能受損。 如果反應爐的狀態超過臨界,就可能過載並導致爐心熔毀。 我們希望降低爐心熔毀的機率,並正確管理反應爐的狀態。

以下三個任務都與撰寫程式碼來維持理想的反應爐狀態有關。

1. 檢查臨界狀態

控制系統要做的第一件事,就是檢查反應爐是否_處於臨界平衡_。 當反應爐符合下列條件時,就稱為處於臨界平衡:

  • 溫度低於 800 K。
  • 每秒發出的中子數大於 500。
  • 溫度與每秒發出中子數的乘積小於 500000。

實作函式 is_criticality_balanced(),它接受以克耳文為單位測量的 temperature 和 neutrons_emitted 兩個參數,並在符合臨界條件時回傳 True,不符合時回傳 False。

>>> is_criticality_balanced(750, 600)
True

2. 判斷電力輸出範圍

反應爐開始產生電力後,就需要判斷它的效率。 效率可以分為 4 個等級:

  1. green -> 效率 80% 以上,
  2. orange -> 效率低於 80%,但至少 60%,
  3. red -> 效率低於 60%,但仍有 30% 以上,
  4. black -> 效率低於 30%。

百分比數值可以用 (generated_power/theoretical_max_power)*100 計算,其中 generated_power = voltage * current。 請注意,百分比數值通常不是整數,所以務必考慮 < 與 <= 比較運算子的正確用法。

實作函式 reactor_efficiency(<voltage>, <current>, <theoretical_max_power>),它有三個參數:voltage、current 和 theoretical_max_power。 這個函式應該回傳反應爐的效率等級:'green'、'orange'、'red' 或 'black'。

>>> reactor_efficiency(200,50,15000)
'orange'

3. 故障安全機制

你的最後一項任務,是建立一套故障安全機制,以避免過載和爐心熔毀。 這套機制會判斷反應爐是低於、等於還是高於理想的臨界門檻。 接著只要把控制棒插入反應爐(或從中移出),就能提高、降低或停止臨界。

實作函式 fail_safe(),它接受 3 個參數:以克耳文為單位測量的 temperature、neutrons_produced_per_second 和 threshold,並輸出反應爐的狀態碼。

  • 如果 temperature * neutrons_produced_per_second 小於 threshold 的 90%,就輸出狀態碼 'LOW',表示必須移出控制棒才能產生電力。

  • 如果 temperature * neutrons_produced_per_second 的值與 threshold 相差在 10% 以內(也就是比門檻低 0 到 10%、正好等於門檻,或比門檻高 0 到 10%),反應爐就處於_臨界_狀態,應該輸出狀態碼 'NORMAL',表示反應爐處於最佳狀態,且控制棒位於理想位置。

  • 如果 temperature * neutrons_produced_per_second 不在上述範圍內,反應爐正走向爐心熔毀,就必須傳回狀態碼 'DANGER',立即關閉反應爐。

>>> fail_safe(temperature=1000, neutrons_produced_per_second=30, threshold=5000)
'DANGER'
透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Python Exercism

準備好開始 熔毀防制 了嗎?

註冊 Exercism,透過 17 個概念146 個練習 和真人引導來學習並精通 Python,全部免費。