Cannot read property "value" of undefined

So, “item” is not marked on item[0] and that means “item” is undefined. Thats why this error occures.
BUT there is “item = items[x”] BEFORE “if efficiency() > a”

Could you help me with a proper answers please.

The problem here is that you are not defining the variable item inside the function efficiency(). In coding (well, in python at least) you cannot use variables inside a function that you only defined in a while true loop. What you need to do is put the item variable inside the brackets of the function when you call it and when you define it (every instance you use it, it should have item inside the brackets). When you put something inside the brackets of a function it’s called an argument.
I hope this helps,

i tried to identify item in the efficiency but i messed up.

Actually when i turned
efficiency() to efficiency(item)
bestitem() to bestitem(items)

it worked, idk why. i looked from an another level. but still didnt understand why.

Yeah, that is what is what I told you to do (except for the bestitem() bit).
It didn’t work before because you didn’t have the variable item defined in the function. Look here:

def efficiency():
    return item.value / hero.distanceTo(item)

item has not been “defined” as such inside the function. Sure, you defined it in the while True loop, but that doesn’t carry over to the function. By putting item in the brackets () of the function, you are essentially carrying over the variable from the while True loop into the function.

1 Like