2016-12-30 64 views
-2

我試過搜索谷歌和本網站的解釋,我明白了,但已經出現了。我在CodeWars上試圖提出一個問題:正數/負數總和。TypeError:無法訂購的類型:list()> int()CodeWars:正數/負數總和

現在的問題是:

給定一個整數數組。

返回一個數組,其中第一個元素是正數的個數,第二個元素是負數的總和。如果輸入數組爲空或空,返回一個空數組。

而且測試變量是:

Test.describe("Basic tests") 
Test.assert_equals(count_positives_sum_negatives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]),[10,-65]) 
Test.assert_equals(count_positives_sum_negatives([0, 2, 3, 0, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14]),[8,-50]) 
Test.assert_equals(count_positives_sum_negatives([1]),[1,0]) 
Test.assert_equals(count_positives_sum_negatives([-1]),[0,-1]) 
Test.assert_equals(count_positives_sum_negatives([0,0,0,0,0,0,0,0,0]),[0,0]) 
Test.assert_equals(count_positives_sum_negatives([]),[]) 

所以我寫的代碼是:

def count_positives_sum_negatives(arr): 
    if arr == []: 
     return [] 
    positive_counter = 0 
    negative_sum = 0 
    for x in arr: 
     if x > 0: 
      positive_counter += 1 
     elif x < 0: 
      negative_sum += x 
    return (postive_counter, negative_sum) 

當我嘗試的網站,我給出的提交:

基本試驗(10,-65)應該等於[10,-65]

而當我在誘使它在Python解釋器,我得到: 類型錯誤:unorderable類型:列表()> int()函數

所以我知道什麼是錯的第一test.assert_equals列表,這些列表中的第二個元素,但我沒有成功地解決這個問題。任何幫助將非常感激。

+0

列表看起來像你返回'tuple'(在'()'圓括號),但檢查它反對'list'(用'[]'方括號)因此,如果您保持與您使用的數據類型一致,應該解決問題。 –

回答

0

您應該返回即

return [postive_counter, negative_sum] 
相關問題