[Tutor] How to extract a float from an instancemethod call

Sydney Shall s.shall at virginmedia.com
Mon Apr 8 16:49:50 CEST 2013


On 08/04/2013 13:52, Dave Angel wrote:
> On 04/08/2013 08:40 AM, Sydney Shall wrote:
>> Hi,
>> I am  learning Python.
>>
>> I use MAC OSX 10.6.8
>> Python 2.7.3
>>
>> I have been given a project to write a program involving random walks.
>> I have drafted a program which has passed all the static tests, but on
>> testing my program i get the following error message:
>>
>>
>> Traceback (most recent call last):
>>    File "/Users/Sydney/Documents/6.00x Files/Problem
>> Sets/ProblemSet7/ps7 copy.py", line 303, in <module>
>>      testRobotMovement(StandardRobot, RectangularRoom)
>>    File "ps7_verify_movement.py", line 12, in testRobotMovement
>>    File "/Users/Sydney/Documents/6.00x Files/Problem
>> Sets/ProblemSet7/ps7 copy.py", line 285, in updatePositionAndClean
>>      while self.room.isPositionInRoom(self.position) == False:
>>    File "/Users/Sydney/Documents/6.00x Files/Problem
>> Sets/ProblemSet7/ps7 copy.py", line 163, in isPositionInRoom
>>      return self.room[(x,y)] in self.room
>> KeyError: (<bound method Position.getX of <__main__.Position object at
>> 0x4699490>>, <bound method Position.getY of <__main__.Position object at
>> 0x4699490>>)
>>  >>>
>>
>> The program text referred to is the following, I give the whole module,
>> which is part of a larger program.
>>   def isPositionInRoom(self, pos):
>>          """
>>          Return True if pos is inside the room.
>>
>>          pos: a Position object.
>>          returns: True if pos is in the room, False otherwise.
>>          """
>>          x = pos.getX
>>          y = pos.getY
>
> You never show the code for Position.getX and Position.getY, but I'd 
> expect they're methods that take no arguments.  in that case, you need 
> parens in order to call them.
>
>            x = pos.getX()
>            y = pos.getY()
>
>>          return self.room[(x,y)] in self.room
>>
>
> a simple print of x and y could have revealed this as well.
>
>
Dear Dave,
Thanks for  pointing out my mistake. I thought it would be some small 
error on my part.
I had a print statement, but I could not correctly interpret the output.
It solves that problem.



-- 
Sydney Shall


More information about the Tutor mailing list