TypeError: list indices must be integers or slices, not float(TypeError:列表索引必须是整数或切片,而不是浮点数)
问题描述
def convBin():
cont = []
rest = []
dev = []
decimal = []
print("Ingrese el valor a convertir: ")
valor = ast.literal_eval(input())
if isinstance(valor, int):
while valor > 0:
z = valor // 2
resto = valor%2
valor = valor // 2
cont.append(z)
rest.append(resto)
cont.reverse()
rest.reverse()
dev.append(cont[0])
x = 0
while x <= (len(rest) - 1):
dev.append(rest[x])
x += 1
print(" ")
print("Lista de devoluciones: ")
print(dev)
print("")
elif isinstance(valor, float):
a = valor // 1
b = valor % 1
while a > 0:
z = a // 2
resto = a%2
a = a // 2
cont.append(z)
rest.append(resto)
cont.reverse()
rest.pop()
dev.append(cont[1])
for i in rest:
dev.append(rest[i])
print("Inserte el número de error minimo")
num = input()
while num > 0:
dec = b * 1
dec2 = dec//1
dec %= 1
decimal.append(dec2)
print("Parte entera: ")
print(dev)
print("Parte decimal:")
print(num)
else:
print("Ha aparecido un error")
Its shows me an error that i can´t append a float into a list.
After asking you a number, it controls which type of number is it. When it is an int, it doesn´t have any problem. But when it´s a float, it says that it can´t add a float into a list where it´s saved the numbers of the operations made before.
Can someone explain it to me why I can´t append floats into a list or how can I solve the problem?
Traceback (most recent call last): File "Converter.py", line 169, in convBin(); File "Converter.py", line 53, in convBin dev.append(rest[i]) TypeError: list indices must be integers or slices, not float
Thanks.
for i in rest
will give you the actual item in the list, and not index. From your code, it seems that you want to append the value. But actually, you are treating the value as index again, and trying to fetch it from the array.
for i in rest:
dev.append(rest[i])
Fix:
Just change above to:
dev.extend(rest)
But this code, takes a value from rest, then again uses that value as an index, and if that value i
turns out to be a float
, it throws an exception.
You haven't mentioned which line gives you this error. But I think it must be this one. It might give many other unexpected errors like array out of bound
, etc
This is the error I get, if I run your code for valor = 18.5
https://ideone.com/HGagLb
Traceback (most recent call last): File "./prog.py", line 71, in File "./prog.py", line 51, in convBin TypeError: list indices must be integers or slices, not float
Difference between the above example, and the one below(from your code, where you handle int
):
x = 0
while x <= (len(rest) - 1):
dev.append(rest[x])
x += 1
is that, in the first case, i is actually the item(int or float) in the list rest, while in the later one, it's a valid index.
这篇关于TypeError:列表索引必须是整数或切片,而不是浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError:列表索引必须是整数或切片,而不是浮点数


基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01