Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NoTest错误:ValueError:字典更新序列元素#0的长度为4;2是必需的_Python_Function_Dictionary_Nose - Fatal编程技术网

Python NoTest错误:ValueError:字典更新序列元素#0的长度为4;2是必需的

Python NoTest错误:ValueError:字典更新序列元素#0的长度为4;2是必需的,python,function,dictionary,nose,Python,Function,Dictionary,Nose,我是一个非常不识时务的人,这是w/r/t python 2.7,我正在努力学习python the Hard Way()——下面的文件名为ex47_tests.py,我遇到的错误与运行nosetests到我工作的目录有关 根据nosetests,错误来自test\u map()行west处的函数。添加路径({'east',start})并声明:ValueError:element#0处的字典更新序列长度为4;2是必需的,但我无法理解问题所在。。。以下是测试文件: from nose.tools

我是一个非常不识时务的人,这是w/r/t python 2.7,我正在努力学习python the Hard Way()——下面的文件名为ex47_tests.py,我遇到的错误与运行
nosetests
到我工作的目录有关

根据
nosetests
,错误来自
test\u map()
west处的函数。添加路径({'east',start})
并声明:
ValueError:element#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文件包含
Room
类,该类具有
添加路径
功能(方法?):

我已经检查过几次了,我已经成功地运行了
west.add_path({'east',start})
在game.py文件中的代码,但是当我运行
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
>d={}
>>>d.update({'east','start'})
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:字典更新序列元素#0的长度为5;2是必需的
为了更清楚地了解该错误,如果您去解释器检查该错误的类型:

注意“east”和“start”之间的逗号

>>> print(type({'east', 'start'}))
<type 'set'>
>>> print(type({'east': 'start'}))
<type 'dict'>
打印(键入({'east','start'})) 注意“east”和“start”之间的冒号

>>> print(type({'east', 'start'}))
<type 'set'>
>>> print(type({'east': 'start'}))
<type 'dict'>
打印(类型({'east':'start'}))
Hi idjaw,nosetests告诉我错误的来源,因此很容易找到,我理解您产生的错误,但我想我不明白为什么在测试
west时添加路径({'east':start})
在game.py中我没有收到错误。我在“更新”之前查看了
west.path
,在
update
之后查看了
west.path
,我成功地看到房间对象进入了空的{}。嘿@jmb277!:)再仔细看看你写的东西。您编写了这样的代码:
{'east':start}
。看看我在我的解决方案中写的第一行,它指出了这个bug,它写为:
{'east',start}
。注意east和start之间的逗号。这就是bug。嗨,idjaw,我认真地看了你写下的那两段代码片段,我想知道我是否被b/c控制了,我完全错过了
vs
的不同之处-它现在起作用了,我真的很感谢你的耐心-谢谢。@jmb277干杯,伙计。这可不容易抓住。:)祝你学习顺利。坚持下去。