Day 8 - Beginner - Function Parameters & Caesar Cipher

會議記錄

時間:2021年2月15日 20:30~21:00
與會人員:@sky @廖玉米 @筑筑_廖 (依筆劃順序)
旁聽人員:@DotTw erickhchow @fengyu_wu @speedthunder Tim(依字母順序)
半導讀:Sky(前一天確認與會人員時,以為沒有初學者,所以沒有特別準備導讀資料)


8-78. Day 8 Goals: what we will make by the end of the day

本章主題:
Functions with Inputs
Arguments & Parameters

8-79. Functions with Inputs

def my_function(something): # something => 參數 Parameter: the name of the data that's being passed in
	#do this with something

my_function(123) # 123 => 值 Argument: the actual value of the data

8-80. Positional vs. Keyword Arguments

# Positional Arguments vs. Keyword Arguments
def greet_with(name, location):
	# do this with something

# Positional Arguments
greet_with(1, 2)

# Keyword Arguments
greet_with(name=1, location=2)
or
greet_with(location=2, name=1)

8-81. [Interactive Coding Exercise] Paint Area Calculator

Ceil 無條件進位

Angela 是用數學 Ceiling 無條件進位(有餘數時無條件進位)的做法。我當時看到題目時,是用有餘數時 +1 的方式處理。

import math
number_of_cans = math.ceil(area/cover)

建議:建立自己的 Cheat Sheet

在整個課程的進行中,建議大家邊上課、邊建立一個自己的 Cheat Sheet。
以 math.ceil(D1/D2) 為例,Angela 的 Cheat Sheet 就沒有寫。

建立自己的 Cheat Sheet,除了比較熟悉(因為上過課,寫的是自己熟悉的例子),看到時很快回想起之外;因為是自己整理的,也能比較快找到。

每次學到新的資料型態、函式、語法…(不管是課程內或課程外),就可以依照類別,整理到自己的 Cheat Sheet 中。

如果你有這樣做,建議這三個放一起:

無條件捨去小數、四捨五入、無條件進位

課外補充:Python if then 短語法

# C 語法
<condition> ? <expression1> : <expression2>
# Python 語法
<expression1> if <condition> else <expression2>

參考資料:One line if statement in Python (ternary conditional operator) - Python Central

8-82. [Interactive Coding Exercise] Prime Number Checker

Angela 的 for loop,一定會從頭到尾跑完。

例如:88888,即使除以2時就知道不是質數,但還是會繼續除以3、除以4…一直到除以88887,改用 while 好一些。

補充:有關 for loop 和 while loop 迴圈的討論,請參考 Day 7 的討論。

8-83. Caesar Cipher Part 1 - Encryption

8-84. Caesar Cipher Part 2 - Decryption
8-85. Caesar Cipher Part 3 - Reorganising our Code
8-86. Caesar Cipher Part 4 - User Experience Improvements & Final Touches

參數命名避免設定一樣(指函式中的參數,和主程式打算要傳入函式的參數),易造成混淆。

List find index: List.index() method

x = fruits.index("cherry")

補充:可參考

複習:將 List 中的值,加到 String中

String += List[index]

問題:Angela 的code,有 a 的時候(List index = 0),往左 shift 5格(-5)時,為什麼沒有 index < 0 的問題?

因為 python 的 List 已經做處理了。如果 index 是負值時,會從 List 最後面往回數

參考資料:
https://www.quora.com/What-is-negative-index-in-Python

提醒:有些應注意事項,可能 Angela 考量到時間長度、或課程安排順序,沒有及時提到。我們如果看到,就會補充,但不代表課程的程式碼不好,可能只是時候未到,所以沒提。

以這章的題目為例,8-83 程式中的一些問題,在後面就陸續補充說明了。
而迴圈中的 break,則尚未提及,未來談到時,可能會說明原因。