### Lab9-1 (ex9-1) ### def take_attendance(classroom, who_is_here): """ classroom, tuple who_is_here, tuple Checks if every item in classroom is in who_is_here And prints their name if so. Returns "finished taking attendance" """ for kid in classroom: if kid in who_is_here: print(kid) return "finished taking attendance" ### Lab9-2 (ex9-2) ### classroom = ("John", "Mary", "Jomes") who_is_here = ("Mary", ) take_attendance(classroom, who_is_here) ### Lab9-3 ### >>> print(take_attendance(classroom, who_is_here)) ### Lab9-4 (ex9-3) ### def hello(): print("Hello!") hello() def sayHi(name, title): print(name + title + "你好!") sayHi("王小明", "同學") ### Lab9-5 (ex9-4) ### def sayHi(name, title): print(name + title + "你好!") sayHi("王小明", title = "同學") sayHi(title = "同學", name = "王小明") sayHi(title = "同學", "王小明") ### Lab9-6 (ex9-5) ### def get_word_length(word1, word2): word = word1 + word2 return len(word) print("this never gets printed") get_word_length("Hi!", "Python") ### Lab9-7 (ex9-6) ### def get_word_length(word1, word2): word = word1 + word2 return len(word) print("this never gets printed") length1 = get_word_length("Rob", "Banks") length2 = get_word_length("Barbie", "Kenn") length3 = get_word_length("Holly", "Jolley") print("One name is", length1, "letters long.") print("Another name is", length2, "letters long.") print("The final name is", length3, "letters long.") ### Lab9-8 (ex9-7) ### def add_sub(n1, n2): add = n1 + n2 sub = n1 - n2 return (add, sub) (a, b) = add_sub(3, 4) ### Lab9-9 (ex9-8) ### # 有傳回值和沒有傳回值的函式 def say_name(kid): print(kid) def show_kid(kid): return kid say_name("Dora") show_kid("Ellie") print(say_name("Frank")) print(show_kid("Gus")) ### Lab9-10 (ex9-9) ### # 在主程式和函式中定義相同名稱的變數 def fairy_tale(): peter = 5 print(peter) peter = 30 fairy_tale() print(peter) ### Lab9-11 (ex9-10) ### # 在函式內初始化變數 def e(): v = 5 print(v) v = 1 e() ### Lab9-12 (ex9-11) ### # 函式內部可存取外部主程式的變數 def f(): print(v) v= 1 f() ### Lab9-13 (ex9-12) ### # 函式內部可讀取多個外部主程式的變數 def g(): print(v + x) v = 1 x = 2 g() ### Lab9-14 (ex9-13) ### # 函式內部可讀取多個外部主程式的變數 def h(): v += 5 v= 1 h() ### Lab9-15 (ex9-14) ### # 同樣的變數名稱,不同的有效範圍 def odd_or_even(num): num = num%2 if num == 1: return "odd" else: return "even" num = 4 print(odd_or_even(num)) odd_or_even(5) ### Lab9-16 (ex9-15) ### # 巢狀函式的例子:主程式中可以呼叫 sing(),不可直接呼叫 stop() def sing(): def stop(line): print("STOP", line) stop("it's hammer time") stop("in the name of love") stop("hey, what’s that sound") sing() ### Lab9-17 (ex9-16) ### # 將函式物件作為參數傳入另一個函式 def sandwich(kind_of_sandwich): print("--------") print(kind_of_sandwich()) print("--------") def blt(): my_blt = " bacon\n lettuce\n tomato" return my_blt def breakfast(): my_ec = " eggegg\n cheese" return my_ec print(sandwich(blt)) ### Lab9-18 (ex9-17) ### # 傳回函式物件 def grumpy(): print("I am a grumpy cat:") def no_n_times(n): print("No", n, "times...") def no_m_more_times(m): print("...and no", m, "more times") for i in range(n + m): print("no") return no_m_more_times return no_n_times grumpy()(4)(2) ### Lab9-19 ### >>> print("no newline") >>> print("yes newline\n") ### Lab9-20 (ex9-18) ### # 移除換行符號 word = "bird\n" print(word) word = word.replace("\n", "") print(word) ### Lab9-21 (ex9-19) ### # 從檔案裡讀取好友姓名及電話 def read_file(file): """ file, a file object Starting from the first line, it reads every 2 lines and stores them in a tuple. Starting from the second line, it reads every 2 lines and stores them in a tuple. Returns a tuple of the two tuples. """ first_every_2 = () second_every_2 = () line_count = 1 for line in file: stripped_line = line.replace("\n","") if line_count % 2 == 1: first_every_2 += (stripped_line, ) elif line_count % 2 == 0: second_every_2 += (stripped_line, ) line_count += 1 return (first_every_2, second_every_2) ### Lab9-22 (ex9-20) ### # 移除電話號碼裡的空格、連字號、括號 def sanitize(some_tuple): """ phones, a tuple of strings Removes all spaces, dashes, and open/closed parentheses in each string Returns a tuple with cleaned up string elements """ clean_string = () for st in some_tuple: st = st.replace(" ", "") st = st.replace("-", "") st = st.replace("(", "") st = st.replace(")", "") clean_string += (st, ) return clean_string ### Lab9-23 (friend.txt) ### # 好友姓名與電話號碼的測試檔 (friend.txt) Ana 801-456-789 Ben 609 4567890 Cory (206)-345-2619 Danny 6095648765 ### Lab9-23 (ex9-21) ### # 從檔案裡讀取好友姓名與電話號碼 friends_file = open("friends.txt") (names, phones) = read_file(friends_file) print(names) print(phones) clean_phones = sanitize(phones) print(clean_phones) friends_file.close() ### Lab9-24 (map_areacodes_states.txt) ### # 區碼對應州別的測試檔 (map_areacodes_states.txt) 201 New Jersey 202 Washington D.C. 203 Connecticut 204 Canada – Manitoba