2015-10-12 63 views
-1

我想要做的是將國家與前兩個國家進行比較,看看它們是否完全不同。我很難將這些值存儲在一個列表中,然後進行比較。我已經嘗試了字符串,但看起來似乎沒有正確。不支持的操作數類型爲 - :'list'和'int':如何比較列表項?

不支持的操作數類型爲 - :'list'和'int' 是我收到的錯誤。任何提示解決此問題?

def purchase(amount, day, month, country): 
    global history, owed, last_country 
    owed += amount 
    history += [(days_in_months(month - 1) + day)] 
    last_country += [country] 
    if history[len(history) - 2] > history[len(history) - 1]: 
     return str(error) 
    elif all_three_different(country, last_country[len(last_country)-1], last_country[len(last_country-2)]) == True: 
     return str(error) 
    else: 
     return True 
+1

請提供一些輸入(即列表)和預期產出 – Pynchia

+0

而回溯,和參數的值和全局變量。我可以在那裏看到四個加法操作,並且您沒有向我們提供任何有關導致問題的信息 –

+0

國家將作爲字符串輸入,例如「法國」 – holla

回答

1

您正試圖從這裏列表中減去2:

last_country[len(last_country-2)] 

注意括號! last_country-2表達式爲len()調用。你可能打算這樣做:

last_country[len(last_country)-2] 

你並不需要使用長度在所有雖然;只是負指數:

last_country[-2] 

這會得到完全相同的值;列表中的1但是最後一個值。編制索引時,負指數會自動從列表長度中減去。

你不需要做的其他事情是使用== True;這就是if/elif聲明已經爲你做的;剛剛離開那關:

if history[-2] > history[-1]: 
    return str(error) 
elif all_three_different(country, last_country[-1], last_country[-2]): 
    return str(error) 
else: 
    return True 
+0

鷹眼:) – The6thSense

相關問題