###### tags: `Python` # 第十三堂課練習20211212 ```python= f = open('test.csv','r') #開啟test檔案,檔案必須與程式放一起 f.readline() #讀取標題列,之後不會重複讀取此列 score = [] #宣告List,儲存成績 chn = 0 #國文分數 math = 0 #數學分數 num = 0 #記錄列數 for line in f: #line:每次讀取一列 data = line.strip().split(',') #strip()去除換行符號\n,#split:將資料分割 chn += int(data[1]) #儲存國文成績 math += int(data[2]) #儲存數學成績 num += 1 #每次讀完一列+1 f.close() #關閉test檔案 print('平均分數: ') score.append(chn) #append:新增資料,國文成績放入score score.append(math) #append:新增資料,數學成績放入score for s in score: print('%.1f' %(s / num), end = ' ') ``` :::spoiler 執行結果     :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up