# 4.小明的電子帳簿(PythonDemo4) ###### tags: `PythonDemo` **分類:檔案處理** **說明** 很幸運地,小明的工作越做越大,因此小明開始把他的查帳本升級成電子化的系統。 請你按照資料裡把每個廚師的所得印出來並且最後算出餐廳的總額和平均每個廚師的所得。 檔案的格式一般如下 先會有廚師的名字,然後下一行一個整數就是他賺的錢。 讀到檔案結束爲止。 [文字檔下載](https://jgirl.ddns.net/files/3092eAccount.rar) **Input Format** 輸入一個字串表示檔案的名字。例如 0.txt 可讀取../app/0.txt的文字檔內容 0.txt 的内容如下 John 10000 Bill 20000 Duck 30000 Donald 40000 **Output Format** chef (空格)(廚師名字)(空格)廚師所得 ... Total: 總額 Avg: 平均廚師所得(以二位小數為準) --- ```python= n = input() f1 = open('../app/' + n, 'r', encoding = 'big5') txt = f1.readlines() f1.close() lstName = [] lstPrice = [] for i in range(len(txt)): aaa = txt[i].strip('\n') if i % 2 == 0: lstName.append(aaa) if i % 2 == 1: lstPrice.append(int(aaa)) for i in range(len(lstName)): print("chef", lstName[i], lstPrice[i]) avg = sum(lstPrice)/len(lstPrice) print("Total:", sum(lstPrice)) print("Avg:", "%.2f" % avg) ```