2012-07-24 38 views
1

我从做ex47学习Python硬盘的方式This is the exercise 这是我的代码:的Python:导入moduel属性错误,LPTHW ex47:

from nose.tools import* 
from ex47.game import Room 

def test_room(): 

    gold = Room("GoldRoom", 

       """This room has gold in it you can grab. there's a door to the north.""") 

    assert_equal(gold.name, "GoldRoom") 
    assert_equal(gold.paths, {}) 

def test_room_paths(): 

    center = Room("Center", "Test room in the center.") 
    north = Room("North", "test room in the north.") 
    south = Room("south", "test room in the south.") 

    center.add_paths({'north': north, 'south': south}) 
    assert_equal(center.go('north'), north) 
    assert_equal(center.go('south'), south) 


def test_map(): 

    start = Room("Start", "You can go west and down a hole.") 
    west = Room("Trees", "There are trees here, you can go east.") 
    down = Room("Dungeon", "It's dark down here, you can go up.") 


    start.add_paths({'west': west, 'down': down}) 
    west.add_paths({'east': start}) 
    down.add_paths({'up': start}) 


    assert_equal(start.go('west'), west) 
    asset_equal(start.go('west').go('east'), start) 
    assert_equal(start.go('down').go('up'), start) 

然而,当测试该代码使用nosetest这就是我得到:

Traceback <most recent call last>: 
File "c:\Python31\lib\site-packages\nose\case.py", line 197,in runtest 
    self.test<*self.arg> 
File "C:\path\Ex47\skeleton\tests\ex47_tests.py", line 28, in test_map 
    start.add_paths<{'west': west, 'down':down>} 
AttributeError: 'Room' object has no attribute 'add_paths' 

Ran 3 tests in 0.030s 

FAILED <errors=2> 

但这似乎是荒谬的,因为add_paths在test_ro工作正常om_paths()。 这使我疯了(谷歌并没有帮助既不抄袭本书的代码)。

请帮忙!!

我使用python 3.1,Windows 7的

请不要使用太多的编程行话。(我很新的节目)。

这里是game.py的代码,以防万一你需要它。

class Room(object): 



def __init__(self, name, description): 
    self.name = name 
    self.description = description 
    self.paths = {} 

    def go(self, direction): 
     return self.paths.get(direction, None) 


    def add_paths(self, paths): 
     self.paths.update(paths)  

回答

3

幽州,“add_paths在test_room_paths工作的罚款()”,但你假设的测试是在你写的顺序执行。它们通常按字母顺序运行,这意味着test_room_paths尚未执行。

你的房间代码看起来不正确,你有下def add_paths缩进,这意味着add_paths__init__本地定义的函数,而不是在你的类的另一种方法。确保您班级中的所有def关键字排列整齐。

+0

谢谢!我不相信我犯了这样一个愚蠢的错误!对不起! – user1544624 2012-07-24 10:20:13