#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    10

    Exclamation 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:
    Code:
    def objlist():
        """Returns a list of the robot's sensors and what they         detect."""
        xN,yN=r.sensor(N)
        xW,yW=r.sensor(W)
        xE,yE=r.sensor(E)
        xS,yS=r.sensor(S)
        m=(xN,yN),(xW,yW),(xE,yE),(xS,yS)
        objlist= list(m)
        return objlist
    
    print objlist()
    print objlist.index(max(objlist))
    and this gives me:
    [(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.
    B.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    You have confused the function with the value it returns, this is what you wanted to do:
    print objlist().index(max(objlist()))
    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()
    print ans.index(max(ans))

    it's a bit easier to read and would probably be faster in situations where speed was important.

    grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    I can call min(objlist) and max(objlist) no problem
    Really? That should give you "TypeError: iteration over non-sequence"...


    Code:
    def objlist():
       objlist= list(m)
    Ack. objlist is already used as the function name. This will just overwrite it, or produce other confusing problems.

    Code:
    m = (...)
    m isn't a helpful variable name.

    Code:
        m = [(xN,yN),(xW,yW),(xE,yE),(xS,yS)]
    There, now m is a list.

    Code:
        xN,yN = r.sensor(N)
    How does that represent something detected by a sensor?
    Surely 'North' has at least one fixed coordinate?

    Code:
    def objlist():
        xN,yN=r.sensor(N)
    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"...

    Code:
    print objlist.index(max(objlist))
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    10

    maybe try...


    hello!
    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
    You have confused the function with the value it returns, this is what you wanted to do:
    print objlist().index(max(objlist()))
    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()
    print ans.index(max(ans))

    it's a bit easier to read and would probably be faster in situations where speed was important.

    grim
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    i did actually try objlist().index(max(objlist())), and it doesn't work.
    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.

    grim
    Last edited by Grim Archon; April 5th, 2005 at 03:56 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    10

    Thanks guys


    As my deadline approaches, I've admitted defeat. Thanks for your input though, it was very helpful.

IMN logo majestic logo threadwatch logo seochat tools logo