nosetest error: ValueError: dictionary update sequence element #0 has length 4; 2 is required

nosetest error: ValueError: dictionary update sequence element #0 has length 4; 2 is required

我是一个菜鸟,这是 w/r/t python 2.7 和我正在做的练习 学习 Python 困难的方法 (link to ex47) - 下面的文件名为 ex47_tests.py,我得到的错误与 运行ning nosetests 相关,我正在工作的目录。

根据 nosetests,错误来自行 west.add_paths({'east', start}) 处的 test_map() 函数,它指出:ValueError: dictionary update sequence at element #0 has length 4; 2 is required 但我无法理解问题所在。 .. 这是测试文件:

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)
    assert_equal(start.go('west').go('east'), start)
    assert_equal(start.go('down').go('up'), start)

作为参考,game.py 文件包含具有 add_paths 函数(方法?)的 Room class:

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)

我已经复习了好几次,我已经成功地 运行 game.py 文件中 west.add_paths({'east', start}) 的代码,但是当我 运行 nosetests我不断收到同样的错误。在代码中发生错误的地方,我的解释是 west 包含一个空的 {} 应该 update 没有问题,不是吗?有人可以提供一些关于为什么这不起作用以及错误来自何处的见解吗?

非常感谢。

代码中的错误来自此调用:

west.add_paths({'east', start})

要对此进行的更正是,您要使用字典而不是集合进行更新:

west.add_paths({'east': start})

当您尝试使用集合更新字典时,以下示例会重现此错误:

>>> d = {}
>>> d.update({'east','start'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 5; 2 is required

为了更清楚地说明这个错误,如果你去找你的解释器并检查它的类型:

注意 'east' 和 'start'

之间的逗号
>>> print(type({'east', 'start'}))
<type 'set'>

注意 'east' 和 'start'

之间的冒号
>>> print(type({'east': 'start'}))
<type 'dict'>