Need help with Python

Need help with Python
Hello,
I am ignorant in Python, and I would have to program a node
Can you help me
Values are numbers.

X1 (list), Y1 (list), E (list), M (an integer)
Values (output)
X2 (list), Y2 (list), L (list)
Formulas:
If X1 = E
Then
X2 = X1
Y2 = Y1 + M
L = Y2
Else
X2 = X1 + M
Y2 = Y1
L = X2
End
*************** French
Besoins d’aide en Python
Bonjour,
Je suis ignorant en Python, et j’aurais pesoins de programmer un nœud
Pouvez-vous m’aider
Les valeurs sont des nombres.
Valeurs (entrée)
X1 (list) , Y1 (list) , E (list) , M (un nombre entier)
Valeurs (sortie)
X2 (list) , Y2 (list) , L (list)
Formules :
If X1 = E
Then
X2= X1
Y2= Y1 + M
L= Y2
Else
X2= X1 +M
Y2=Y1
L=X2
Fin

Get started then come back when you get stuck. There are many examples on this forum you can follow, plus stack overflow, the Dynamo primer, python.org etc etc. Autodidacts reign supreme.

Ps. You’ve practically written the script in python, you just need to format it

2 Likes

Thank you for this answer which will not make much progress for me …

Hello, this page could help to get started with Python nodes:
http://dynamoprimer.com/en/09_Custom-Nodes/9-4_Python.html
And here is the way to present a code into forum posts:

Courage! :slight_smile:

Hello,

It’s not the courage that’s missing, but the time …

Thanks for the link, but I have already been there.

The big problem for me is the translation with google! (Although I found links in france for python).

Learning Python shouldn’t be a problem but this forum is not the place for it. But you will find good examples such as this one:
http://dynamobim.org/forums/topic/if-then-else-in-dynamo/
You can then paste your code here to get further advice from experts…

If time is the problem and Dynamo is un-chartered territory for you, then its clearly the wrong tool for the job.

Unfortunately learning is nothing but time consuming I’m afraid and as much of an investment as you make it.

Like @Yna_Db said, this is not a Python forum. However, we’re happy to help you out when you are stuck, but first you have to give it a go. The reply from @Thomas_Mahon earlier was actually encouraging you to do just this, your psuedocode is pretty close so converting to Python isn’t that hard, so give it a go and then come back after you have and we can help point out where it’s going wrong.

Cheers,
Dan

Hello,

One test but does not function line 40

1 Like

Hi @Alain_Hamel ,
you’re missing an indent on lines 40 and 43 (because you’re inside a loop).

1 Like

thank you for your reply.
Is the amendment correct?
I have another error:
Avertissement:IronPythonEvaluator.EvaluateIronPythonScript l’opération a échoué.
Traceback (most recent call last):
File “”, line 40, in
IndexError: index out of range: 1

@Alain_Hamel can you please paste your code here ?

Like that?

code 1.py (975 Bytes)

Like this :relaxed:

1 Like

You had an issue line 21 :

appending IN[2] to a list adds the whole list as a nested list. The length of that list will be 1. I just changed that…

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
dataEnteringNode = IN

#The inputs to this node will be stored as a list in the IN variable.
A = IN[0]
B = IN[1]
E = IN[2]
M = IN[3]

listX1 = []
listY1 = []

listX2 = []
listY2 = []

listE = []

listE  = E

#convert lists
if hasattr(A, '__iter__'):
	for i in range(0,len(A)):
		listX1.append(A[i])
else:
	listX1.append(A)

if hasattr(B, '__iter__'):
	for i in range(0,len(B)):
		listY1.append(B[i])
else:
	listY1.append(B)

# matched lists
lists = []

for i in range(0,len(listX1)):
	if listX1[i] == listE[i]:
		listX2.append(listX1[i])
		listY2.append(listY1[i]+M)
	else:
		listY2.append(listX1[i])
		listX2.append(listY1[i]+M)


lists.append(listX2)
lists.append(listY2)


#Assign your output to the OUT variable
OUT = lists
1 Like

Thank you for your help.
This is just a tabulation problem?

I’ll finish the code

My pleasure :slight_smile:

you had the tabulation problem on line 41 and 43 at first.
The other issue was the way you defined listE :
listE.append(E) is different from listE = E.

Yes, I just wanted to mimic the list code X1.append (A)

thank you,
The completed code:


2 Likes

I understand. If you had mimicked it all the way it would have also worked:

   if hasattr(E, '__iter__'):
    	for i in range(0,len(E)):
    		listE.append(E[i])
    else:
    	listE.append(E)

This part checks whether E is a list :

   if hasattr(E, '__iter__'):

if it is then it appends each element of the list to listE one by one.
if E is a single element and not a list, it will be appended to listE.

Anyway congrats on your first python code! :slight_smile: !!