## wk08_1026_ch06_元組與字典 ## 【inclass practice】 實作6 西元2021年是牛年。請你開發一個程式: 當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。 ```python list1=["鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬",] a=int(input("輸入西元出生年:")) print(list1[a%12-4]) ``` 輸入西元出生年:2021 牛 ```python list1=[3,2,1,5] list2=sorted(list1,reverse=True) print(list2) ``` [5, 3, 2, 1] ```python scores=[21,63,91,20] scores.sort(reverse=False) print(scores) ``` [20, 21, 63, 91] ```python scores=[21,63,91,20] scores_sorted=sorted(scores) print(scores_sorted) ``` [20, 21, 63, 91] ```python scores=[21,63,91,20] scores=sorted(scores) print(scores) ``` [20, 21, 63, 91] 實作2 輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項 ```python a=input("輸入喜歡的水果:") fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"] b= a in fruit if b == True : print("包含,在串列中的第",fruit.index(a)+1,"項") else : print("不包含") ``` 輸入喜歡的水果:橘子 包含,在串列中的第 3 項 ```python #刪除指定串列元素 \<remove> fruits = ["香蕉","蘋果","橘子","鳳梨","西瓜"] while True: fruit=input("不輸入喜歡的水果: enter nothing to end") if fruit =="": break b=fruit in fruits if b==True : a=fruits.index(fruit) fruits.pop(a) print(fruits) ``` 不輸入喜歡的水果: enter nothing to end西瓜 不輸入喜歡的水果: enter nothing to end香蕉 不輸入喜歡的水果: enter nothing to end水梨 不輸入喜歡的水果: enter nothing to end ['蘋果', '橘子', '鳳梨'] {補充} list + while 練習 1. 計算總和:編寫一個程式,使用迴圈計算一個整數串列的所有元素的總和。 2. 找出最大值:編寫一個程式,在一個整數串列中查找並返回最大的數字。 3. 偶數過濾:編寫一個程式,接受一個整數串列,然後返回一個只包含偶數的新串列。 4. 反轉串列:編寫一個程式,將一個字元串列反轉,例如,將['a', 'b', 'c']轉換為['c', 'b', 'a']。 5. 查找元素:編寫一個程式,接受一個整數串列和一個目標數字,返回目標數字在串列中的索引,如果它不存在,返回-1。 ```python # 4. fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"] list1=fruit[::-1] fruit.reverse() print(fruit) print(list1) ``` ['西瓜', '鳳梨', '橘子', '蘋果', '香蕉'] ['西瓜', '鳳梨', '橘子', '蘋果', '香蕉'] ```python # 2. list2=[21,63,91,20] themax = list2[0] for n in list2 : if n > themax : themax = n print(themax,max(list2)) #找出最小值 list2=[21,63,91,20] themax = list2[0] for n in list2 : if n < themax : themax = n print(themax,min(list2)) ``` 91 91 20 20 dictionary ```python # dict1=dict(鍵1=值1 ,鍵2=值2, .... ) # dict1= {鍵1:值1 ,鍵2:值2, .... } dict3 = {"香蕉":20,"蘋果":50,"橘子":50,"香蕉":120} print(dict3) print(dict3["香蕉"])#鍵重複會直接覆蓋 ``` {'香蕉': 120, '蘋果': 50, '橘子': 50} 120 ```python #血型個性查詢 \<dictget> a=input("請輸入血型(A/B/AB/0)") dict1={"0":"外向","A":"內向","B":"穩重","AB":"偏激"} result=dict1[a] print("查詢結果: ",result) ``` 請輸入血型(A/B/AB/0)A 查詢結果: 內向 ## 【afterclass practice】 (B) 1. 關於字典,下列何者敘述是錯誤的? (A)以「鍵-值」對方式儲存 (B)資料依序排列 (C)可由「鍵」取得「值」 (D)資料隨機排列 ```python # (D)2. d={"香蕉":20, "蘋果":50},print(d[0]) 的結果為何? (A)香蕉 (B)20 (C)50 (D)產生錯誤 d={"香蕉":20, "蘋果":50} print(d[0]) ``` --------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[2], line 6 1 # (D)2. d={"香蕉":20, "蘋果":50},print(d[0]) 的結果為何? 2 3 #(A)香蕉 (B)20 (C)50 (D)產生錯誤 5 d={"香蕉":20, "蘋果":50} ----> 6 print(d[0]) KeyError: 0 ```python # (B)3. d={"香蕉":20, "蘋果":50},print(d["香蕉"])) 的結果為何?(A)香蕉 (B)20 (C)50 (D)產生錯誤 d={"香蕉":20, "蘋果":50} print(d["香蕉"]) ``` 20 ```python # (C)4. d={"香蕉":20, "蘋果":50},print(d.get("巴樂", 60)) 的結果為何? (A)20 (B)50 (C)60 (D)None d={"香蕉":20, "蘋果":50} print(d.get("巴樂", 60)) ``` 60 ```python # (A)5. d={"香蕉":20, "蘋果":50},程式「d["巴樂"]=60」的作用為: (A)新增資料 (B)修改資料 (C)取得資料 (D)刪除資料 d={"香蕉":20, "蘋果":50} d["巴樂"]=60 print(d) ``` {'香蕉': 20, '蘋果': 50, '巴樂': 60} ```python # (A)6. d={"香蕉":20, "蘋果":50},程式「d.clear()」的作用為: (A)刪除所有元素 (B)刪除一個元素 (C)刪除字典 (D)以上皆非 d={"香蕉":20, "蘋果":50} d.clear() print(d) ``` {} ```python # (C)7. d={"香蕉":20, "蘋果":50},print("香蕉" in d) 的結果為何? (A)20 (B)50 (C)True (D)False d={"香蕉":20, "蘋果":50} print("香蕉" in d) ``` True (C)8. 下列哪一個功能可取得字典中所有「值」? (A)in (B)keys (C)values (D)items (D)9. 下列哪一個功能可取得字典中所有「鍵」及所有「值」? (A)in (B)keys (C)values (D)items ```python # (B)10. d={"香蕉":20},print(d.setdefault("巴樂")) 的結果為何? (A)20 (B)None (C)巴樂 (D)產生錯誤 d={"香蕉":20} print(d.setdefault("巴樂")) ``` None