Re: [Tutor] list question
On Tue, Aug 11, 2009 at 5:47 PM, proportio...@msn.com wrote: hi i am currently doing the 'livewires' python tutorial lesson 5. i am making a little game that makes a few squares chase a circle around a little grid. i can get the 1 square acting on 1 variable to come up, but the tutorial now wants me to create a list of variables that act under the same class. this is what i have to make the 1 square appear (which works)(i have also cut a lot of the program out to make this email smaller.) class Robot: pass def place_robot(): global robot robot = Robot() robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x and i cant figure out what to write to make the variable 'robot' work as a list, and then follow all the instructions 3 times. this is what i tried to put in. class Robot: pass def place_robot(): global robot r1 = Robot() r2 = Robot() r3 = Robot() robot = [r1,r2,r3] mydict = {} x=random_between(0,63) y=random_between(0,47) mydict[str(robot)] = {'x':x,'y':y} #robot.x=random_between(0,63) # robot.y=random_between(0,47) draw_robot() print robot.x i was under the assumption that the instruction robot.x=random_between(0,63) would return a value 3 times for r1 r2 and r3, but instead i get AttributeError: 'list' object has no attribute 'x'. so i got no idea heh. i hope this makes sense, as im really new to programming. thanks if anyone can help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Ataulla SH web:www.kring.com personal blog:www.ataulla.objectis.net KRING Technologies India Pvt. Ltd. 1st Floor, Tower B, Infinity Towers, DLF II, Gurgaon-122 002 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list question
On Tue, Aug 11, 2009 at 7:17 AM, proportio...@msn.com wrote: hi i am currently doing the 'livewires' python tutorial lesson 5. i am making a little game that makes a few squares chase a circle around a little grid. i can get the 1 square acting on 1 variable to come up, but the tutorial now wants me to create a list of variables that act under the same class. this is what i have to make the 1 square appear (which works)(i have also cut a lot of the program out to make this email smaller.) class Robot: pass def place_robot(): global robot robot = Robot() robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x and i cant figure out what to write to make the variable 'robot' work as a list, and then follow all the instructions 3 times. this is what i tried to put in. class Robot: pass def place_robot(): global robot r1 = Robot() r2 = Robot() r3 = Robot() robot = [r1,r2,r3] robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x i was under the assumption that the instruction robot.x=random_between(0,63) would return a value 3 times for r1 r2 and r3, but instead i get AttributeError: 'list' object has no attribute 'x'. so i got no idea heh. i hope this makes sense, as im really new to programming. I think what you want is: for r in robot: r.x = random_between(0,63) r.y = random_between(0,47) draw_robot() print r.x Now for the why: robot is a list of objects - you declared it such with robot = [r1, r2, r3] (incidentally you could just do robots = [Robot(), Robot(), Robot()] ), and lists don't have x y attributes, which you're trying to access. I presume you're really trying to access the x y attributes of your Robot() class, which is an entirely different object from your list. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list question
if u want to add new attributes into ur list u need to import list object in ur class class customlist(list): x = 'your val' c = customlist() dir(c) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__dict__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', '__weakref__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort', *'x'*] c.x 'your val' On Tue, Aug 11, 2009 at 6:25 PM, Wayne sri...@gmail.com wrote: On Tue, Aug 11, 2009 at 7:17 AM, proportio...@msn.com wrote: hi i am currently doing the 'livewires' python tutorial lesson 5. i am making a little game that makes a few squares chase a circle around a little grid. i can get the 1 square acting on 1 variable to come up, but the tutorial now wants me to create a list of variables that act under the same class. this is what i have to make the 1 square appear (which works)(i have also cut a lot of the program out to make this email smaller.) class Robot: pass def place_robot(): global robot robot = Robot() robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x and i cant figure out what to write to make the variable 'robot' work as a list, and then follow all the instructions 3 times. this is what i tried to put in. class Robot: pass def place_robot(): global robot r1 = Robot() r2 = Robot() r3 = Robot() robot = [r1,r2,r3] robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x i was under the assumption that the instruction robot.x=random_between(0,63) would return a value 3 times for r1 r2 and r3, but instead i get AttributeError: 'list' object has no attribute 'x'. so i got no idea heh. i hope this makes sense, as im really new to programming. I think what you want is: for r in robot: r.x = random_between(0,63) r.y = random_between(0,47) draw_robot() print r.x Now for the why: robot is a list of objects - you declared it such with robot = [r1, r2, r3] (incidentally you could just do robots = [Robot(), Robot(), Robot()] ), and lists don't have x y attributes, which you're trying to access. I presume you're really trying to access the x y attributes of your Robot() class, which is an entirely different object from your list. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Ataulla SH web:www.kring.com personal blog:www.ataulla.objectis.net KRING Technologies India Pvt. Ltd. 1st Floor, Tower B, Infinity Towers, DLF II, Gurgaon-122 002 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list question
Wayne wrote: On Tue, Aug 11, 2009 at 7:17 AM, proportio...@msn.com wrote: hi i am currently doing the 'livewires' python tutorial lesson 5. i am making a little game that makes a few squares chase a circle around a little grid. i can get the 1 square acting on 1 variable to come up, but the tutorial now wants me to create a list of variables that act under the same class. this is what i have to make the 1 square appear (which works)(i have also cut a lot of the program out to make this email smaller.) class Robot: pass def place_robot(): global robot robot = Robot() robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x and i cant figure out what to write to make the variable 'robot' work as a list, and then follow all the instructions 3 times. this is what i tried to put in. class Robot: pass def place_robot(): global robot r1 = Robot() r2 = Robot() r3 = Robot() robot = [r1,r2,r3] robot.x=random_between(0,63) robot.y=random_between(0,47) draw_robot() print robot.x i was under the assumption that the instruction robot.x=random_between(0,63) would return a value 3 times for r1 r2 and r3, but instead i get AttributeError: 'list' object has no attribute 'x'. so i got no idea heh. i hope this makes sense, as im really new to programming. I think what you want is: for r in robot: r.x = random_between(0,63) r.y = random_between(0,47) draw_robot() print r.x Now for the why: robot is a list of objects - you declared it such with robot = [r1, r2, r3] (incidentally you could just do robots = [Robot(), Robot(), Robot()] ), and lists don't have x y attributes, which you're trying to access. I presume you're really trying to access the x y attributes of your Robot() class, which is an entirely different object from your list. HTH, Wayne This illustrates the danger of global variables. The draw_robot() function probably accesses the global variable robot, which has now changed its meaning. So probably it should be a method of the Robot class, in which case you'd use r.draw_robot() Alternatively, if it must be a function rather than a method, you should add a parameter to it, and call it as draw_robot(r) If this were my code, I'd probably have created a new global variable robots, indicating that it's now a list. And change the function place_robot() to place_robots(). DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List-question
On 19/12/05, Øyvind [EMAIL PROTECTED] wrote: I have one function that finds some values. Then I want that function to find new values based on the values it found first. However, by just looping, it starts on an eternal job. As illustrated in: list = [1,2,3] list2 = list list2 [1, 2, 3] for i in list: ... print i ... list2.append(4) ... 1 2 3 4 4 4 and it will forever continue with 4's. Why would list be expanded with the values of list2? How can I copy the result from one list, and do things with the list without getting it to expand? Because they point to the same thing. Type list2 is list after your other code and see. You want list2 to be a COPY of list not a pointer to it. Do this by using list2 = list.copy() Slices create a copy, so a shortcut is: list2 = list[:] Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor