为什么我会收到错误:typeerror:typer of float;
我正在尝试编码打印相对和绝对错误的计算器。在算法的最后一步中,我需要创建一个新列表来保留Y的新值,但我会遇到错误:
typeError:不能将序列乘以“ float”类型'
这是代码:
#Metodo de minimos cuadrados
#Crear lista
lstx = []
lsty = []
#Numero de elementos den el input
n = int(input("¿Cuantos valores desea ingresar?"))
#Arreglo de x
for i in range (0,n):
x = float(input("Ingrese los valores de x: "))
lstx.append(float(x))
sumx = sum(lstx)
#Arreglo de y
for a in range (0,n):
y = float(input("Ingrese los valores de y: "))
lsty.append(float(y))
sumy = sum(lsty)
#Generamos lista de xy
lstxy = [x*y for x,y in zip(lstx,lsty)]
sumxy = sum(lstxy)
#Generamos lista de x^2
lst2 = [n**2 for n in lstx]
sum2 = sum(lst2)
#Calcular la pendiente
m = (sumxy - (sumx*sumy)/n) / (sum2 - (sumx*sumx)/n)
#Calcular la intercepción
promx = sumx / len(lstx)
promy = sumy / len(lsty)
b = promy - (m*promx)
#Obtener los nuevos valores de y
new = [(m*lstx)+b]
newY = []
for item in new:
newY.append(float(item))
I'm trying to code a calculator that prints relative and absolute error,. In the final step of the algorithm I need to create a new list to retain the new values of Y but I'm getting the error:
TypeError: can't multiply sequence by non-int of type 'float'
This is the code:
#Metodo de minimos cuadrados
#Crear lista
lstx = []
lsty = []
#Numero de elementos den el input
n = int(input("¿Cuantos valores desea ingresar?"))
#Arreglo de x
for i in range (0,n):
x = float(input("Ingrese los valores de x: "))
lstx.append(float(x))
sumx = sum(lstx)
#Arreglo de y
for a in range (0,n):
y = float(input("Ingrese los valores de y: "))
lsty.append(float(y))
sumy = sum(lsty)
#Generamos lista de xy
lstxy = [x*y for x,y in zip(lstx,lsty)]
sumxy = sum(lstxy)
#Generamos lista de x^2
lst2 = [n**2 for n in lstx]
sum2 = sum(lst2)
#Calcular la pendiente
m = (sumxy - (sumx*sumy)/n) / (sum2 - (sumx*sumx)/n)
#Calcular la intercepción
promx = sumx / len(lstx)
promy = sumy / len(lsty)
b = promy - (m*promx)
#Obtener los nuevos valores de y
new = [(m*lstx)+b]
newY = []
for item in new:
newY.append(float(item))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于Python具有许多不同的数据类型,例如INT,Float,List ...-您无法在任何地方使用他们的协作。您可以使用type()函数获取变量的数据类型,然后查看问题。
“ M”和“ B”变量是浮子。 “ LSTX”是列表。您可以乘以 integer(不带有列表),并且您不能在列表和不列表之间进行 plus 操作。
As Python has a lot of different data types, such as int, float, list... - you can't use their collaboration wherever you want. You can use type() function to get a data type of your variable and see where is a problem.
"m" and "b" variables are floats. "lstx" is list. You can multiply integer with list (not float with list) and you can't do plus operation between list and not list.