TypeError: list indices must be integers or slices, not float
时间: 2023-09-29 10:02:43 浏览: 161
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
5星 · 资源好评率100%
This error occurs when you try to use a float value as an index for a list.
For example:
```
my_list = [1, 2, 3, 4, 5]
print(my_list[2.5]) # This will raise a TypeError
```
To fix this error, make sure you are using integer values as list indices. If you need to use a float value, you can convert it to an integer using the `int()` function:
```
my_list = [1, 2, 3, 4, 5]
print(my_list[int(2.5)]) # This will print 3
```
阅读全文