3

I am doing my python assignment, but there is an error when I wanted to test the case above.

Here is my code:

def evalTerm(env, t):
    if type(t) == Node:
        for label in t:
            children = t[label]
            if label == 'Number':
                t = children[0]
                return t

            elif label == 'Add':
                t1 = children[0]
                v1 = evalTerm(env, t1)
                t2 = children[1]
                v2 = evalTerm(env, t2)
                return v1 + v2

            elif label == 'Multiply':
                t1 = children[0]
                v1 = evalTerm(env, t1)
                t2 = children[1]
                v2 = evalTerm(env, t2)
                return v1 * v2


            elif label == 'Variable':
                x = children[0]
                if x in env:
                    return env[x]
                else:
                    print(x + " is unbound")
                    exit()

            elif label == 'Int':
                f = children[0]

                v = evalTerm[env, f]
                if v == 'True':
                    return 1
                elif v == 'False':
                    return 0

            elif label == 'Parens':
                x = children[0]
                v = evalTerm(env, x)
                return v

    elif type(f) == Leaf:
        if f == 'True':
            return 'True'
        if f == 'False':
            return 'False'

When I test it by using:

evalTerm({}, {'Int': ['True']})

it gives an error:

'function' object is not subscriptable

How could I fix it?

3
  • By the way:in my code, I wrote: Node = dict; Leaf = str Commented Feb 16, 2015 at 23:14
  • Is that your actual indentation? For python, we need to see the exact indentation you're using. Commented Feb 16, 2015 at 23:16
  • Is the error message pointing to a specific line? The error is most likely there... Also include that Node = dict, Leaf = str thing in your question! Commented Feb 16, 2015 at 23:23

1 Answer 1

4

It's impossible to say for sure without the full traceback, but most likely this is your problem:

v = evalTerm[env, f]

evalTerm is a recursive function that you would need to call (note the additional () instead of [] ):

v = evalTerm(env, f)

Square brackets [ ] in Python are used for subscription or indexing - that means, for example addressing a value in a dictionary by its key, or an item in a list by its index.

If you get the exception 'foo' object is not subscriptable that means that you tried to use subscription for an object of type 'foo' that doesn't support it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.