TypeError: unsupported operand type(s) for -: ‘str’ and ‘float’

Hello,

I have a problem with this code:

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Place your code below this line
Lista_P=IN[0]
np=IN[1]
ns=IN[2]
bpX = IN[3][0][0]
bpY = IN[3][1][0]
#bpX = IN[3][0]
#bpY = IN[3][1]
Sec=[]
a=0
b=0
c=-1
Pontos_T=[]
for a in range(0,np,ns):
	c=c+1
	b=0
	Pontos=[]
	for b in range(ns):
		xp = (Lista_P[a][2]) - bpX   # <- Line 28
		yp = (Lista_P[a][1]) - bpY
		zp = (Lista_P[a][3])
		Pontos.append(Point.ByCoordinates(xp,yp,zp))
		b=b+1
		a=a+1
	Pontos_T.insert(c,Pontos)


# Assign your output to the OUT variable.
OUT = Pontos_T

i have this erros mensage:

Warning: PythonEvaluator operation failed.
Traceback (most recent call last):
File " < string> ", line 28, in < module>
TypeError: unsupported operand type(s) for -: ‘str’ and ‘float’

thanks

I’m not sure which line 28 is… and I have no idea what your inputs are…

But it could be the in range.
is ns a number or a string? It needs to be a number, you can cast it to be a number if it’s “23” not 23…
int(ns) would fix that. IF that’s the issue.

Otherwise

You’re mixing strings + numbers… or trying to use a string where you should use a number.
Check what your inputs are and make sure they’re the correct type.

Hi

I have a CivilReport

image

And this Dynamo

Thanks

image

What type of data is coming out of here?

See the colour?
Text (strings) are orange
Numbers are blue.

As I said in my first reply… Make sure the inputs are the correct type.
Convert your strings to numbers.
You can do this before you feed it into the python node or within the code.
float() or int()

1 Like

But i have number in properties excel.

And you’re importing to an entirely different program.

If it’s orange in Dynamo it’s a string.

Check the ‘read as strings’ input for the read excel node.

use Default Value, checked

Consider ‘false’ as the input, which will mean the node will try to keep a number format instead of a string.

No resolve.

I transform string list in number

Try something along the lines of:

Listx=IN[0]
Lista_P =
for i in Listx:
for j in i:
Lista_P.append(int(i))

1 Like

You could add a helper function near the top of your code
nc = lambda n: eval(n) if isinstance(n, str) else n

then modify the lines

Your code uses multiples of the same variable (like this b=0, for b in range..., b=b+1) which could result in unexpected results
As far as I can tell you want to process all the points in lists of ns chunks.
You could do this very efficiently by mapping a point function over all the excel rows and then breaking the list into chunks

def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i : i + n]

def point_bc(lst):
    return Point.ByCoordinates(lst[2] - bpX, lst[1] - bpY, lst[3])

Lista_P = IN[0]  # Excel
ns = IN[1]  # Step
bpX = IN[2][0][0]  # E/W
bpY = IN[2][1][0]  # N/S

pontos = list(map(point_bc, Lista_P))

Pontos_T = list(chunks(pontos, ns))

# Assign your output to the OUT variable.
OUT = Pontos_T