'Why do I get the error: "TypeError: can't multiply sequence by non-int of type 'float'"?

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))
        


Solution 1:[1]

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.

print(type(m))
print(type(lstx))
print(type(b))

"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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oleg Lysytskyi