2014-06-22 26 views
1

我想要去除從文件中得分,在一個有序列表,我可以打印出第1,第2和第3個地方安排他們,這裏是我的代碼:使得從一個txt列表數組

scores = [] 
result_f = open("results.txt") 
for line in result_f: 
    (name, score) = line.split() 
    scores.append = (float(score)) 
result_f.close() 
print("The highest scores were: ") 
print(score[0]) 
print(score[1]) 
print(score[2]) 

控制檯誤差蟒給出爲:

文件 「C:\ Python34 \ surfingscores.py」,第5行,在 scores.append =(浮動(評分)) AttributeError的: '列表' 對象屬性 '追加'是隻讀的

回答

2

scores.append是一個函數。除了使用

scores.append = float(score) 

的請嘗試使用

scores.append = float(score) 

或交替

scores[len(scores):] = float(score) 
1

你需要

scores.append(float(score)) 

目前您正試圖分配浮點值到scores列表的屬性append,這不是您想要的。由於scores列表(這是追加到列表的函數)的append屬性是隻讀的,因此顯式保護您的當前代碼正在執行的操作,因此會拋出此錯誤。

>>> scores = [] 
>>> scores.append 
<built-in method append of list object at 0x7f08964f9638> 
1

scores.append是分數的函數/方法。

您需要進行函數調用。

scores.append(float(score))