2017-10-13 40 views
0

我無法理解第二個函數引發的錯誤。當我通過第二功能黑客我實現打印(X)給我: [ '安娜',74.0],[ '比爾',65.0],[ 'CAL',98.0] 無列表錯誤後的無類型

由於到這個沒有,我無法遍歷列表並將其設置爲L.什麼導致這沒有?這是另外3個功能的粘貼箱。 code here!。任何意見或推動正確的方向非常感謝。


def string_avg(L): 
'''(list of str) -> list of list 
Given a list of strings where each string has the format: 
'name, grade, grade, grade, ...' return a new list of 
lists where each inner list has the format : 
[name (str), average grade (float)] 
Requirement: This function should be 1 line long. 

>>> string_avg(['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 
98']) 
[['Anna', 74.0], ['Bill', 65.0], ['Cal', 98.0]] 
''' 
return(average_grade((string_list(L)))) 


def string_avg_update(L): 
'''(list of str) -> NoneType 
Given a list of strings where each string has the format: 
'name, grade, grade, grade, ...' update the given 
list of strs to be a list of floats where each item 
is the average of the corresponding numbers in the 
string. Note this function does NOT RETURN the list. 
>>> L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 
>>> string_avg_update(L) 
>>> L 
[74.0, 65.0, 98.0] 
''' 

x = string_avg(L)  

我打算代碼的最後一個函數是:

Say I changed the last function code to: 
x = string_avg(L)  

for i in range(0,len(x)): 
    L[i] = x[i][1] 

但它說我找不到nonetype的LEN,爲什麼我的X一無類型?

+0

假如你運行你寫到文檔的文檔測試?結果是什麼? –

+0

'x = string_avg(L)'設置一個局部變量'x',當函數結束時這個變量不會被使用。 –

+0

@MichaelButscher我得到了['安娜,50,92,80','比爾,60,70','Cal,98.5,100,95.5,98']回到我的第二個功能(這與輸入)以及所有其他功能的預期結果。我的目標是找出什麼導致沒有,然後設置L [每個索引] = x [每個索引] [1]。 –

回答

0

average_grade()不返回一個列表,但只打印並返回None

+0

這正是問題所在。它隨我而去。非常感謝! –

相關問題