gpt4 book ai didi

python - Nose 测试错误: ValueError: dictionary update sequence element #0 has length 4; 2 is required

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:37 24 4
gpt4 key购买 nike

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

根据nosetests,错误来自test_map()函数的west.add_paths({'east', start})行code> 并指出: ValueError: 元素 #0 处的字典更新序列长度为 4; 2 是必需的,但我不明白问题是什么......这是测试文件:

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 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 包含一个空的 {} ,应该可以毫无问题地更新,不是吗?有人可以提供一些关于为什么这不起作用以及错误来自何处的见解吗?

非常感谢。

最佳答案

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

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'>

关于python - Nose 测试错误: ValueError: dictionary update sequence element #0 has length 4; 2 is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com