Python Unexpected Token ':' Error

Hey all. I’m trying to give three input lists of IN[0], IN[1], and IN[2] to a Python Node (which its code is shown below) and receive an output which is a calculated number based on information received from those lists. The code has been compiled and it ran just fine outside of dynamo with test lists and no error was received. But, I am getting an "unexpected token ‘:’ " error when I run the code. Could you please help me with this?

These inputs are also shown in the pictures below:

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

# FUNCTIONS =====================
def mat_dict(mat):
    mat_d = {}
    for item in mat:
        mat_d[item[0]]=item[1]
    return (mat_d)

def match_mat(surf,mat_d):
    prdct_lst =[]
    for item in surf:
        a = []
        for i in range(len(item[0])):
            b = []
            area = item[1][i]
            coef = mat_d[item[0][i]]
            for coef_item in coef:
                b.append(area*coef_item)
            a.append(b)
        prdct_lst.append(a)
    return(prdct_lst)
#=====================================

dataEnteringNode = IN

mat_in = IN[0]
flr_in = IN[1]
wll_in = IN[2]

material_dictionary = mat_dict(mat_in)
wall_list = match_mat(wll_in,material_dictionary)
floor_list = match_mat(flr_in,material_dictionary)

room_denom = []
for i in range (len(wall_list)):
    room = [0,0,0,0,0,0,0]
    a = [0,0,0,0,0,0,0]
    b = [0,0,0,0,0,0,0]
    for m in range (7):
        for j in range (len(wall_list[i])):
            a[m] += wall_list[i][j][m]
        for k in range (len(floor_list[i])):
            b[m] +=  floor_list[i][k][m]
    for m in range (7):
        room[m] = a[m]+b[m]
    room_denom.append(room)

OUT = room_denom

Thanks in advance for your help.

I would double check everywhere there is an : and make sure everything around it is properly formatted. Since the error didn’t give a line, you could try commenting out chunks of code at a time to figure out where the error is. You can comment out a section with """ """:

"""
everything here
is a comment
"""
1 Like

He is using it as a python dictionary which he calls it as such in the next function, so that isn’t the issue.

Try making a copy of your python node, and remove parts of the code bit by bit until you have located the “:” causing the issue.
Sometimes for me, I have had to just retype that part of the code, and somehow it has resolved my issue.

It looks like some of your range commands have a space between “range” and the parentheses. This may be causing the issue. The colons appear to be in the correct places at first glance.

I don’t think so :confused: Tested it in IronPython and Python 3.7, didn’t have an issue.

Hmm, wasn’t aware that spaces were allowed before the parentheses; thanks for letting me know! I’ll be sure to post again if I think of something else

Is there any chance that there is a : inside one of your inputs? Reaching a bit here, but if you are inputting things that aren’t strings that may have a : inside of them, they could cause an error when being used as a dictionary key.

Thank you all so much for your help.

@kennyb6 I went through every single input item to see if there was a any : in any of them and I didnt find any.

@andre.abotnes Im going to try your method of debugging. I’ll report back.

Thanks again for your time guys

Kenny, this worked. I pinned down the problem and I found out that I have been leaving out a ‘)’ while typing the code in dynamo. It turns out that this code is fine and it was a typo that was causing the error.

Thanks again everyone for your help.

2 Likes