Please help me dynamo python

Enable Python support and load DesignScript library

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
from operator import itemgetter

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

allpts = IN[0]
otherpts = IN[1]

Place your code below this line

firstpt = allpts[0]
visitedpts = [firstpt]
currentpts = visitedpts[-1]
del otherpts[0]
cur_other_dis =
target_list_values =
l = len(otherpts)

#I want to repeat the following work!! but I don’t know how to write

for i in range(l):
dis = round(Geometry.DistanceTo(currentpts, otherpts[i]))
cur_other_dis.append(dis)

target_list_index = [i for i, x in enumerate(cur_other_dis) if x == 350]
target_list_len = len(target_list_index)

if target_list_len == 1:
target_list_values.append(otherpts[0])
target_list_value = target_list_values[0]

elif target_list_len == 2:
for z in target_list_index:
target_list_values.append(otherpts[z])
target_list_values = sorted(target_list_values)
target_list_value = target_list_values[0]

visitedpts.append(target_list_value)
otherpts.remove(target_list_value)

currentpts = visitedpts[-1]
target_list_values =
target_list_index =
cur_other_dis =
l = len(otherpts)

OUT = [firstpt, visitedpts, currentpts, otherpts, target_list_values, target_list_index, target_list_len, cur_other_dis, l]

Hi @naoto,

So you can get the help you need a little faster, please can you explain the problem you are facing ie. What you are trying to do, and what issues you are experiencing and also format your code with the </> button so it is a little more legible.

1 Like

Thank you for teaching me !!!
I don’t know why the error happens
Thank you in advance!!

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from operator import itemgetter
# The inputs to this node will be stored as a list in the IN variables.
allpts = IN[0]
otherpts = IN[1]
# Place your code below this line
firstpt = allpts[0]
visitedpts = [firstpt]
currentpts = visitedpts[-1]
del otherpts[0]
cur_other_dis = []
target_list_values = []
l = len(otherpts)
dis = []

for w in range(9):
	for i in range(l):
		dis = round(Geometry.DistanceTo(currentpts, otherpts[i]))
		cur_other_dis.append(dis)
	target_list_index = [i for i, x in enumerate(cur_other_dis) if x == 350]
	target_list_len = len(target_list_index)
#候補が一個の場合
	
	if target_list_len == 2:
		for z in target_list_index:
			target_list_values.append(otherpts[z])
    	target_list_values = sorted(target_list_values)
        target_list_value = target_list_values[0]	
        visitedpts.append(target_list_value)
        otherpts.Remove(target_list_value)
	elif target_list_len == 1:
		target_list_values.append(otherpts[0])
		target_list_value = target_list_values[0]
	
	currentpts = visitedpts[-1]
	cur_other_dis = []
	target_list_values = []
	target_list_index = []
	
	l = len(otherpts)
	
OUT = [firstpt, visitedpts, currentpts, otherpts, target_list_value, target_list_values, target_list_len, cur_other_dis, target_list_len]

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
unexpected token ‘elif’

Maybe capital R in Remove right before elif??

I tried changing to a smaller r, but the error still occurred:すすり泣き:
Thank you for giving me advice!

You have mixed tabs and spaces when indenting the if statement which has confused the interpreter. Although you can use either in Python, its best to be consistent. PEP-8 style is 4 spaces

Below is the code with the tabs replaced by four spaces. It throws different errors now about target_list_value, but I don’t know what point data you want to feed it.

Hope this helps,
Thomas

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from operator import itemgetter
# The inputs to this node will be stored as a list in the IN variables.
allpts = IN[0]
otherpts = IN[1]
# Place your code below this line
firstpt = allpts[0]
visitedpts = [firstpt]
currentpts = visitedpts[-1]
del otherpts[0]
cur_other_dis = []
target_list_values = []
l = len(otherpts)
dis = []

for w in range(9):
    for i in range(l):
        dis = round(Geometry.DistanceTo(currentpts, otherpts[i]))
        cur_other_dis.append(dis)
    target_list_index = [i for i, x in enumerate(cur_other_dis) if x == 350]
    target_list_len = len(target_list_index)
#候補が一個の場合
    
    if target_list_len == 2:
        for z in target_list_index:
            target_list_values.append(otherpts[z])
        target_list_values = sorted(target_list_values)
        target_list_value = target_list_values[0]	
        visitedpts.append(target_list_value)
        otherpts.Remove(target_list_value)
    elif target_list_len == 1:
        target_list_values.append(otherpts[0])
        target_list_value = target_list_values[0]
    
    currentpts = visitedpts[-1]
    cur_other_dis = []
    target_list_values = []
    target_list_index = []
    
    l = len(otherpts)
    
OUT = [firstpt, visitedpts, currentpts, otherpts, target_list_value, target_list_values, target_list_len, cur_other_dis, target_list_len]
2 Likes

In case you face the same error as @Thomas_Corrie after applying his solution:

The NameError occurs because target_list_value is not being initiated if target_list_len is neither 1 nor 2.

Adding an else statement will catch all other cases.
This can help you with if/elif/else statements.

2 Likes

Thank you so, so much!!
This code worked correctly!!!
Thank you for the kind thoughts!:スマイル:

Thank you for giving me advice!
This issue has been resolved!
Thank you for your kindness:にやにや: