I have problem with this loop and if statement in dynamo (python scripts)

I have problem with this loop and if statement in dynamo (python scripts)

somphon
Explorer Explorer
3,814 Views
4 Replies
Message 1 of 5

I have problem with this loop and if statement in dynamo (python scripts)

somphon
Explorer
Explorer

I am beginner in writing python scripts in revit dynamo. I really need to used both for loop and if else statement together in one node to identify rebar size but when I run these code

 

import clr
import math
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import*

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
    
doc = DocumentManager.Instance.CurrentDBDocument

#The inputs to this node will be stored as a list in the IN variables.
Size = IN[0]

Bartype = []

for i in Size:
    p = []

    if Size == "9":
        p = "RB-9"
        
    elif Size == "12":
        p = "DB-12"
        
    elif Size == "16":
        p = "DB-16"
        
    elif Size == "20":
        p = "DB-20"
        
    elif Size == "25":
        p = "DB-25"
        
    elif Size == "28":
        p = "DB-28"
        
    elif Size == "32":
        p = "DB-32"
        
    elif Size == "40":
        p = "DB-40"
    
    else:
        p = "n/a"
        
        Bartype.append(p)

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

 

I obtain only n/a, while my input data have those numbers (my data is import from excel)

 
0 Likes
3,815 Views
4 Replies
Replies (4)
Message 2 of 5

TheRealChrisHildebran
Advocate
Advocate

Not fluent in Python but thought this may be a Type disparity. 

 

Is your 'Size' variable a string or an integer?

 

The values you are looking for are in quotes whcih indicates to me a string Type.

0 Likes
Message 3 of 5

somphon
Explorer
Explorer

Input data from get.itematindex is in var[] and output I looking for is string because I will used to match with my family name

0 Likes
Message 4 of 5

somphon
Explorer
Explorer

To be precise my data from excel come into double[] and python scripts I write intended to compared between each double[] data from my excel with certain integer such as Size == 12: This mean that IN[0] that goes into Size will be tested with if statement if its true I would like to print a string "DB-12". so according to my data

 

Size = Double[] contains 16,20,25,32,12,9

goes into my python scripts my expected result is

BarType = String[] contains "DB-16","DB-20","DB-25","DB-32","DB-12","DB-9"

 

but it somehow didnt detect any syntax but result came out to be BarType = "n/a","n/a","n/a","n/a","n/a","n/a" for all test which shouldn't happen. n/a is false statement where int in double[] didnt match any of true statement. My true statement have 12 16 20 25 28 32 40 which normally should match since I intentionally used only number that match with true statement. which I presume that my python scripts will add input as double[] and output as string[]

0 Likes
Message 5 of 5

nur91m
Advocate
Advocate

Your code is wrong. It should be like this

 

for i in Size:
    p = []
    if i == "9":  # not Size == "9". "i" is the item you shoud check in every loop iteration.
        p = "RB-9"

 

If it helped, please check it as solved.

0 Likes