问题描述
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?
回溯(最近一次调用最后一次):文件Converter.py",第 169 行,在convBin();文件Converter.py",第 53 行,在 convBin 中dev.append(rest[i]) TypeError:列表索引必须是整数或切片,不浮动
谢谢.
推荐答案
for i in rest
会给你列表中的实际项目,而不是索引.从您的代码来看,您似乎想附加该值.但实际上,您又将该值视为索引,并尝试从数组中获取它.
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])
修正:
只需将上面更改为:
dev.extend(rest)
但是这段代码,从rest中获取一个值,然后再次使用该值作为索引,如果该值i
结果是一个float
,它会抛出一个例外.
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.
你没有提到哪一行给你这个错误.但我想应该是这个.它可能会导致许多其他意外错误,例如 array out of bound
等
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
这是我得到的错误,如果我为 valor = 18.5 运行你的代码
This is the error I get, if I run your code for valor = 18.5
回溯(最近一次调用最后一次):文件./prog.py",第 71 行,在文件./prog.py",第 51 行,在 convBin TypeError: list索引必须是整数或切片,而不是浮点
上面的例子和下面的例子之间的区别(来自你的代码,你处理 int
的地方):
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
就是说,在第一种情况下,i 实际上是列表 rest 中的项目(int 或 float),而在后一种情况下,它是一个有效的索引.
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.
这篇关于类型错误:列表索引必须是整数或切片,而不是浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!