# wk12_ch07_函式與模組 高承紘 7.1 自訂函式 自訂函式 參數預設值 變數有效範圍 7.2 數值函式 數值函式整理 指數、商數、餘數及四捨六入 最大值、最小值、總和及排序 7.3 字串函式 字串函式整理 連接及分割字串 檢查起始或結束字串 字串排版相關函式 搜尋即取代字串 ## 【inclass practice】 ### {範例} 大樂透 1A2B 函式1 : 數字相同+位置相同 函式2 : 數字相同 + 位置不同 ```python import random as r list1=r.sample(range(1,50),7) special=list1.pop() print(f"開獎號碼 : {list1} \n特別號:{special}") ``` 開獎號碼 : [14, 2, 49, 43, 8, 35] 特別號:22 ```python import random as r def evaluate(guess): x,y=0,0 for i in range(4): if(guess[i]==secret[i]): x+=1 elif(guess[i] in secret): y+=1 return x,y secret="".join(r.sample("123456789",4)) while True: guess=input("enter four number: ") a,b=evaluate(guess) print(f"{a}A{b}B") if(a==4): break ``` enter four number: 1234 1A2B enter four number: 5678 0A1B enter four number: 1235 1A2B enter four number: 1236 1A2B enter four number: 1237 1A2B enter four number: 1238 1A3B enter four number: 8321 1A3B enter four number: 2831 1A3B enter four number: 3821 0A4B enter four number: 8312 2A2B enter four number: 2318 0A4B enter four number: 3821 0A4B enter four number: 8132 4A0B ## 【afterclass practice】 尋找期末專題主題 {綜合演練} ### 實作3: 以十二小時(上午、下午)顯示現在時刻。 ```python import time def showNow(): time1 = time.localtime() h = time1.tm_hour if h >= 12: now = "下午" h -= 12 else: now = "上午" print(time1) print(f"現在時間 : {now} {h}點{time1.tm_min}分{time1.tm_sec}秒") showNow() ``` time.struct_time(tm_year=2023, tm_mon=11, tm_mday=28, tm_hour=22, tm_min=22, tm_sec=34, tm_wday=1, tm_yday=332, tm_isdst=0) 現在時間 : 下午 10點22分34秒 ### 實作4: 小華媽媽上菜市場買菜,找錢時她希望在1元、5元、10元、50元硬幣中找回最少的硬幣。小華就利用自訂函式幫媽媽寫了一個程式。 ```python def r(a,b): if(b>a): return 0 c=a-b d=int(c/50) e=int((c%50)/10) f=int((c%10)/5) g=int((c%5)) return c,d,e,f,g h=0 while(h==0): b=int(input("要買多少錢的東西? ")) a=int(input("給了多少錢? ")) if(r(a,b)==0): print("給的錢不夠,請重新輸入") else: h=1 c,d,e,f,g=r(a,b) print(f"找回{d}個50元、{e}個10元、{f}個5元、{g}個1元,合計{c}元,共{d+e+f+g}個硬幣") ``` 要買多少錢的東西? 161 給了多少錢? 100 給的錢不夠,請重新輸入 要買多少錢的東西? 161 給了多少錢? 500 找回6個50元、3個10元、1個5元、4個1元,合計339元,共14個硬幣 ### 自己做的1A1B ```python def b(a,b): c=0 if(int(a/1000)==int(b/1000)): c+=1 if(int((a%1000)/100)==int((b%1000)/100)): c+=1 if(int((a%100)/10)==int((b%100)/10)): c+=1 if(int((a%10))==int((b%10))): c+=1 return c def g(a,b): c=0 if((int(a/1000)==int((b%1000)/100)) or (int(a/1000)==int((b%100)/10)) or (int(a/1000)==int(b%10))): c+=1 if((int((a%1000)/100)==int(b/1000)) or (int((a%1000)/100)==int((b%100)/10)) or (int((a%1000)/100)==int(b%10))): c+=1 if((int((a%100)/10)==int(b/1000)) or (int((a%100)/10)==int((b%1000)/100)) or (int((a%100)/10)==int(b%10))): c+=1 if((int((a%10))==int(b/1000)) or (int((a%10))==int((b%1000)/100)) or (int((a%10))==int((b%100)/10))): c+=1 return c import random as r a=r.randint(0,9999) while(g(a,a)!=0): a=r.randint(0,9999) d=False e=False while(d==False): c=int(input("請輸入一個數(0到9999之間)")) while(e==False): if(c>9999 or c<0): print("重輸!") c=int(input("請輸入一個數(0到9999之間)")) else: e=True if(a==c): d=True print("恭喜答對!") else: f=b(c,a) h=g(c,a) print(f"{f}A{h}B") ``` 請輸入一個數(0到9999之間)1234 1A0B 請輸入一個數(0到9999之間)5678 0A2B 請輸入一個數(0到9999之間)9999 0A0B 請輸入一個數(0到9999之間)1506 2A2B 請輸入一個數(0到9999之間)1560 恭喜答對!