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

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

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

thank you Mostafa

but i still have this :

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 …

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

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
4 Likes

many thanks Mostafa!

it was indeed a noob question.

1 Like

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

1 Like

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

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

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