Python OOP-操作对象方法的输出
我试图在一个对象中使用一个方法来操作它,但它总是告诉我数据的类型不匹配 我还在学习,我知道这种问题肯定是每个人在学习过程中迟早都会遇到的,但我找不到一个明确的解释,也找不到像这样的问题。这可能是我需要提高搜索技能的原因Python OOP-操作对象方法的输出,python,oop,Python,Oop,我试图在一个对象中使用一个方法来操作它,但它总是告诉我数据的类型不匹配 我还在学习,我知道这种问题肯定是每个人在学习过程中迟早都会遇到的,但我找不到一个明确的解释,也找不到像这样的问题。这可能是我需要提高搜索技能的原因 def runSimulation(num_robots, speed, width, height, min_coverage, num_trials, robot_type): """ Runs NUM_TRIALS trials of the simulat
def runSimulation(num_robots, speed, width, height, min_coverage, num_trials, robot_type):
"""
Runs NUM_TRIALS trials of the simulation and returns the mean number of
time-steps needed to clean the fraction MIN_COVERAGE of the room.
The simulation is run with NUM_ROBOTS robots of type ROBOT_TYPE, each with
speed SPEED, in a room of dimensions WIDTH x HEIGHT.
num_robots: an int (num_robots > 0)
speed: a float (speed > 0)
width: an int (width > 0)
height: an int (height > 0)
min_coverage: a float (0 <= min_coverage <= 1.0)
num_trials: an int (num_trials > 0)
robot_type: class of robot to be instantiated (e.g. StandardRobot or
RandomWalkRobot)
"""
space = RectangularRoom(width, height)
bots = []
times = []
total = 0
for n in range(num_robots):
bots.append(StandardRobot(space, speed)) #needs change to robot_type
for s in range(num_trials):
test_time = 0
while space.getNumCleanedTiles/(width*height) < min_coverage:
for bot in bots:
bot.updatePositionAndClean()
test_time += 1
times.append(test_time)
for time in times:
total += time
return total / len(times)
当我尝试运行它时,它告诉我无法计算space.getNumCleanedTitles/width*height我没有包括代码的其余部分,因为它并不重要,而且很清楚什么调用做什么。@luther得到了答案,我忘了在getNumCleanedTitles后面加上一个“你似乎在迭代一个emtpy列表。你是对的,我只是忘记了一个s在那里。现在已修复。返回什么类型的space.GetNumCleanedTitles?您忘记调用实例方法。放在GetNumCleanedTitles之后。它是return lenself.cleaned。我现在试着检查一下类型。