无法将列表添加到一起“TypeError:float”对象不可下标

2024-04-19 20:53:19 发布

您现在位置:Python中文网/ 问答频道 /正文

无法确切地找出原因,但在我尝试打印test2导致TypeError: float' object is not subscriptable之前,此代码完全按照预期工作。不管我做什么样的数学或者test的内容,我仍然会得到同样的错误

问题代码:

with open("InputFileData.csv") as file:
   SampleTotalHours = file.readlines();

TotalHours = []

for element in SampleTotalHours:
   hoursAsStrings = element.split(',')
   hoursAsFloats = [float(hourString) for hourString in hoursAsStrings]
   TotalHours.append(sum(hoursAsFloats))

EmployeeNumber = 4
i = 0

TotalHoursInt = []
TotalHoursInt = TotalHours

test = [11, 12, 13]
test2 = TotalHoursInt[1] + test[1]
print(test2[1])

内容“InputFileData.csv”:

40.1,39.7,40,38
36,36,35.5,35.8
40,41.6,40.3,40
20.4,22.8,20,20

输出:

TypeError:float' object is not subscriptable

Tags: csv代码test内容objectisnotfloat