Day 10 - Beginner - Functions with Outputs

記錄預留處:討論完成後,負責記錄的人將在此更新。

預計討論時間:2月21日(週日)晚上8點30分

討論方式一:有新手參與時,以導讀為主,經驗分享與QA為輔
一、Discord:導讀人以視訊分享畫面、與會人以聲音參與討論(書房一)
二、論壇:需要輸入文字時

討論方式二:無新手參與時,以經驗分享為主、QA為輔
一、Discord:與會人以聲音參與討論,主持人場控進度(書房一)
二、論壇:需要輸入文字時

友善提醒:beginner 階段的課程(Day 1~14),每次討論前,建議做的事

一、新手完成各章節課程時,在論壇中該章(Day)回報自己的程式碼網址(repl.it)。

二、有問題可以隨時在論壇中發問,不用等到討論時才提出。

1個讚

10-96. Day 10 Goals: what we will make by the end of the day
沒什麼

10-97. Functions with Outputs

function 的三進化階段

■ 6-57. Defining and Calling Python Functions

1. defining functions
def my_function():
  # 縮排

2. calling functions
my_function()

■ 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

■ 10-97. Functions with Outputs

def my_function(something):
	result = something * 2 # do this with something
	return result

abc = my_function(123)

參考:string method(對語法還不熟時,我覺得 w3schools 中的 string type, string method, list type, list method 應該都開好視窗當參考)

capitalize() - Converts the first character to upper case
title() - Converts the first character of each word to upper case

10-98. Multiple return values
介紹 function 中可以有多個 return

10-99. [Interactive Coding Exercise] Days in Month
沒什麼

10-100. Docstrings
‘’’ function’s usage tooltip… ‘’’
以前沒用過,蠻有用的方式

Quiz 8: Functions Quiz

10-101. Calculator Part 1: Combining Dictionaries and Functions

我看到題目會直接自己做,做完如果有時間,再來看 Angela 的做法。
之前的經驗是:依照她拆解的方式進行,我反而會卡卡的。
她也說過,解法有很多,不用一定要照她的。

■ 建議在看 Angela 拆解成幾節課的方式前,先自己多想幾次解法,如果可以直接全部完成最好。
然後有空時再看看課堂解法,比較一下哪種較好,有沒有值得學習的地方。

■ 既然加減乘除都是兩個數值當參數,我比較喜歡把四個函式整合成一個,並加上計算符號當第三個參數。像這樣:
my_calculator(n1, n2, operation)

■ 直接就做完了,碰到一個輸入的換行問題
有兩個解法:把問句變短,或長問句之間加斷行就可以了。
那 Angela 的 code 為什麼可以?

■ float format 問題


■ 以上是看video前的疑問;以下是看video後的疑問

為什麼 line 56 的方式可以?

operations = {
  "+": add,
  "-": substract,
  "*": multiple,
  "/":divide,
}
...
calculation_function = operations[calculation_symbol] <= ■ line 56

因為在 operations dictionary 中的 value,就是 function 嗎?
用 print(type(operations[“+”])) 來檢查,的確是。
<class ‘function’>

10-102. Print vs. Return
沒什麼

10-103. While Loops, Flags and Recursion

def calculator():
	...
	calculator() # 函式中呼叫自己

我沒有這樣做,是在 while 中處理了

10-104. Calculator Finishing Touches and Bug Fixes
10-105. How to Get a Good Night’s Sleep
沒什麼

JAVA有類似的 是像這樣回嗎 :vulcan_salute:
多分享個我寫隨機密碼時有用到的module: string
直接呼叫string.ascii_letters就不需要把每個字母都丟進list了
當然也有分大小寫

1個讚