[Tutor] How to extract a float from an instancemethod call
Sydney Shall
s.shall at virginmedia.com
Mon Apr 8 14:40:17 CEST 2013
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
return self.room[(x,y)] in self.room
The module that made the call is;
def updatePositionAndClean(self):
"""
Simulate the raise passage of a single time-step.
Move the robot to a new position and mark the tile it is on as
having
been cleaned.
"""
steps = 0
self.position = self.room.getRandomPosition()
print self.position
self.x = self.position.getX
self.y = self.position.getY
print self.room
self.room.cleanTileAtPosition((self.x, self.y))
self.direction = self.getRobotDirection()
print 'Initial direction in degrees is ' , self.direction
self.position = self.position.getNewPosition(self.direction,
self.speed)
steps += 1
while self.room.isPositionInRoom(self.position) == False:
self.direction = Robot.getRobotDirection()
self.position = Position.getNewPosition(self.direction,
self.speed)
steps += 1
self.x = self.position.getX
self.y = self.position.getY
print 'Current position is x,y ', self.x, self.y
print 'Current direction is ' , self.direction + 'Degrees.'
self.room.cleanTileAtPosition((self.x, self.y))
print self.room.isTileCleaned((self.x, self.y))
I do not understand how one extracts a number from an instance method
that should return a number.
I need to convert this number a float to an int to avoid the key error.
But I do not know how to extract the float.
This conclusion I derive from this earlier 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[(int(x), int(y))] in self.room
TypeError: int() argument must be a string or a number, not 'instancemethod'
>>> ================================ RESTART
================================
I would be grateful for any guidance.
--
Sydney Shall
More information about the Tutor
mailing list