关于random.uniform()的错误论据

时间:2014-11-11 19:34:28

标签: python

帮助我,因为我无法找到以下代码的错误:

    def getRandomPosition(self):
    """
    Return a random position inside the room.

    returns: a Position object.
    """
    return Position(random.uniform(0,self.width),random.uniform(0,self.height))

调试器输出:

room.getRandomPosition()                                                        
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ps2a.py", line 145, in getRandomPosition
return Position(random.uniform(0,self.width),random.uniform(0,self.height))
TypeError: random() takes no arguments (2 given)

PS:位置初始化程序的形式为Position(x,y) 有任何想法吗? random.uniform由文档

获取2个参数

实际的调试器输出(pdb):

>>> room=ps2a.RectangularRoom(
... 3,3)
>>> room.getRandomPosition()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ps2a.py", line 145, in getRandomPosition
TypeError: random() takes no arguments (2 given)
>>> random.random()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined
>>> import random
>>> random.uniform(1,3)
2.5039752079488458
>>> room.getRandomPosition()                                                        
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ps2a.py", line 145, in getRandomPosition
return Position(random.uniform(0,self.width),random.uniform(0,self.height))
TypeError: random() takes no arguments (2 given)
>>> import pdb
>>> pdb.run("room.getRandomPosition()")
> <string>(1)<module>()
(Pdb) s
--Call--
> /home/kostas/pythonprojects/L4P5/ProblemSet2/ps2a.py(139)getRandomPosition ()
-> def getRandomPosition(self):
(Pdb) s
> /home/kostas/pythonprojects/L4P5/ProblemSet2/ps2a.py(145)getRandomPosition()
-> return Position(random.uniform(0,self.width),random.uniform(0,self.height))
(Pdb) p self.width
3
(Pdb) p self.height
3
(Pdb) s
TypeError: 'random() takes no arguments (2 given)'
> /home/kostas/pythonprojects/L4P5/ProblemSet2/ps2a.py(145)getRandomPosition()
-> return Position(random.uniform(0,self.width),random.uniform(0,self.height))
(Pdb) 

职位类,问:

class Position(object):
"""
A Position represents a location in a two-dimensional room.
"""
def __init__(self, x, y):
    """
    Initializes a position with coordinates (x, y).
    """
    self.x = x
    self.y = y

def getX(self):
    return self.x

def getY(self):
    return self.y

def getNewPosition(self, angle, speed):
    """
    Computes and returns the new Position after a single clock-tick has
    passed, with this object as the current position, and with the
    specified angle and speed.

    Does NOT test whether the returned position fits inside the room.

    angle: number representing angle in degrees, 0 <= angle < 360
    speed: positive float representing speed

    Returns: a Position object representing the new position.
    """
    old_x, old_y = self.getX(), self.getY()
    angle = float(angle)
    # Compute the change in position
    delta_y = speed * math.cos(math.radians(angle))
    delta_x = speed * math.sin(math.radians(angle))
    # Add that to the existing position
    new_x = old_x + delta_x
    new_y = old_y + delta_y
    return Position(new_x, new_y)

def __str__(self):  
    return "(%0.2f, %0.2f)" % (self.x, self.y)

0 个答案:

没有答案