How to write simple "if elif else" conditional in Python

#1

hi there

i did a simple list.append in python and it works for a define liste inside python. But when im trying to add a input for Dynamo use, it seems i had an issue.

here the code :

layer = IN[0]
styles=[]
for i in layer[]:
    if i == red :
        styles.append("0.13mm")
    elif i == yellow:
        styles.append ("0.18mm")
    	else :
        	styles.append ("0.13mm")


OUT = styles

im sure it an noob question, but help will be appreciate.

thank you

0 Likes

True false switch challenge
#2

0 Likes

#3

Hi @Yien,
you don’t need to put “[]” in :
for i in layer:

0 Likes

#4

thank you Mostafa

but i still have this :

0 Likes

#5

those elements in your list are strings right? If the answer is yes then you should put “” in your conditional statements :

if i == "red":

and so on …

0 Likes

#6

i have strings and numbers.
i just change for i == “red” : and so on…

0 Likes

Update Version Python conditional not working
#7
layer = IN[0]
styles=[]
for i in layer:
	if i == "red" :
		styles.append("0.13mm")
	elif i == "yellow":
		styles.append ("0.18mm")
	else :
		styles.append ("0.13mm")

OUT = styles
2 Likes

Python Nodes Basics
#8

many thanks Mostafa!

it was indeed a noob question.

1 Like

#9

No problem glad I could help getting you started :slight_smile:

0 Likes

#10

i’ll show you later the result, the main goal is to select all line style from Autocad (which is by color or layer) and transfert them to Revit line style.

thank you again

0 Likes

#11

Couple of housekeeping comments. First and foremost, please format code posted using the “```” so that its displayed properly. Secondly please write a clear and concise description in the title of the post. “Python input” is not exactly descriptive.

Thank you.

2 Likes

#12

Some further knowledge for you is that you could do the following to your code so it can adapt without modification to a increased amount of checks in the future(code and image show below).

Code:

#modified by Brendan Cassidy

styles=[]
#loop for main input list
for i in IN[0]:
	temp_list=[]
	
	#loop for the comparison
	for b in IN[1]:
		if i == b[0]:
			temp_list=b[1]
			
	#Checks if list is empty if yes adds 0.00mm to element	
	if temp_list:
		styles.append(temp_list)
	else :
		styles.append("0.00mm")

OUT = styles

Image:

2 Likes

Conditional evaluation of several lists in Python