Python 3.x 错误:内置函数或方法';对象在python中不可下标

Python 3.x 错误:内置函数或方法';对象在python中不可下标,python-3.x,Python 3.x,我发现了错误 内置函数或方法对象不可下标 任何帮助都将不胜感激。 我是python新手,如果这是一个令人尴尬的错误,我很抱歉 常规除法导致浮点运算,您需要整数作为列表索引 将括号更改为括号并使用整数除法: test_yourself = [1, 2, 3, 4, 5, 6, 7, 8, 9] #calculate the mean mean = sum(test_yourself)/len(test_yourself) #calculate the median of list medi

我发现了错误

内置函数或方法对象不可下标

任何帮助都将不胜感激。
我是python新手,如果这是一个令人尴尬的错误,我很抱歉

常规除法导致浮点运算,您需要整数作为列表索引

将括号更改为括号并使用整数除法:

test_yourself = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#calculate the mean

mean = sum(test_yourself)/len(test_yourself)

#calculate the median of list

median = test_yourself[(len[test_yourself] + 1) / 2]
或强制为int:

median = test_yourself[len(test_yourself) // 2]

len
是一个函数,因此您需要使用括号而不是方括号:
test_yourself[(len(test_yourself)+1)/2]
谢谢您的回复。现在它向我展示了一个错误:列表索引必须是整数或片,而不是浮点
median = test_yourself[int(len(test_yourself) / 2)]