python - nosetest error: ValueError: dictionary update sequence element #0 has length 4; 2 is required -
i noob , w/r/t python 2.7 , exercise i'm working through on learn python hard way (link ex47) - file below named ex47_tests.py , error related running nosetests
directory i'm working in.
according nosetests
, error test_map()
function @ line west.add_paths({'east', start})
, states: valueerror: dictionary update sequence @ element #0 has length 4; 2 required
cannot understand problem is... here's test file:
from nose.tools import * ex47.game import room def test_room(): gold = room("goldroom", """this room has gold in can grab. there's door north.""") assert_equal(gold.name, "goldroom") assert_equal(gold.paths, {}) def test_room_paths(): center = room("center", "test room in center.") north = room("north", "test room in north.") south = room("south", "test room in 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 , down hole.") west = room("trees", "there trees here, can go east.") down = room("dungeon", "it's dark down here, 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)
for reference, game.py file contains room
class has add_paths
function (method?):
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)
i've reviewed several times , have run code west.add_paths({'east', start})
within game.py file when run nosetests
i keep getting same error. @ point in code error occurs, interpretation west
contains empty {}
should update
without issue, no? can provide insight why isn't working , error comes from?
thank much.
the bug in code coming call:
west.add_paths({'east', start})
the correction made this, want update dictionary, not set:
west.add_paths({'east': start})
this error reproducible following example when try update dictionary set:
>>> 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 required
to provide more clarity on bug, if go interpreter , check type of this:
notice comma between 'east' , 'start'
>>> print(type({'east', 'start'})) <type 'set'>
notice colon between 'east' , 'start'
>>> print(type({'east': 'start'})) <type 'dict'>
Comments
Post a Comment