April 4th, 2005, 02:28 AM
Has anyone mastered user_def functions?
Help! I'm trying to write a function that calls a series of other functions; each one of these other functions returns a tuple. I get what I want from the function, but I can't index it!
Here it is so far:
and this gives me:
"""Returns a list of the robot's sensors and what they detect."""
[(xN,yN),(xW,yW)....] <--this is good, it's what i want
AttributeError:'function' object has no attribute 'index'
^this is a problem, because:
The AttributeError makes sense, because I've already defined it as a function, but what should I put in front of .index? I can call min(objlist) and max(objlist) no problem, but what should I put to get the index of min and max? Please help-this function will save my *** if i can get it to work right.
April 4th, 2005, 03:59 AM
You have confused the function with the value it returns, this is what you wanted to do:
but doing so much in one line is very ugly IMO and leads to the sort of errors you got.
To avoid confusion - assign the returned tuple to a name then work on that:
ans = objlist()
it's a bit easier to read and would probably be faster in situations where speed was important.
April 4th, 2005, 01:12 PM
Really? That should give you "TypeError: iteration over non-sequence"...
Ack. objlist is already used as the function name. This will just overwrite it, or produce other confusing problems.
m isn't a helpful variable name.
There, now m is a list.
m = [(xN,yN),(xW,yW),(xE,yE),(xS,yS)]
How does that represent something detected by a sensor?
xN,yN = r.sensor(N)
Surely 'North' has at least one fixed coordinate?
Where does N come from? It's not a parameter, you haven't defined it as a local variable, and you haven't said it's a global variable. That should be giving you an error saying "NameError: global name 'N' is not defined"...
max(something) gets the largest item in the list.
From there, putting that into index() will get you...
"The item at the position indicated by the value of the largest item in the list". This makes not much sense, but when the value is a coordinate tuple, it makes even less sense.
What does 'r.sensor(N)' do?
Last edited by sfb; April 4th, 2005 at 01:15 PM.
April 4th, 2005, 01:58 PM
i did actually try objlist().index(max(objlist())), and it doesn't work. yes, it is ugly....what if i were to assign a variable to each tuple, and index that? like if each tuple had it's own letter, and then return objlist as a function of the letters. does that make sense? i'll have to try it when i get home. thanks for your help!
Originally Posted by Grim Archon
April 5th, 2005, 03:54 AM
I does assume that objlist() actually works as planned
Maybe it's for the reasons that sfb mentioned.
Post some tracebacks and we can advise you more.
Last edited by Grim Archon; April 5th, 2005 at 03:56 AM.
April 5th, 2005, 03:53 PM
As my deadline approaches, I've admitted defeat. Thanks for your input though, it was very helpful.