2015-10-12 39 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

相关问题