Hello, there. I can’t get why the code doesn’t allow me to add another key-value position (see last two lines of the code of script.py). Any ideas?
What indication are you getting that you are not being allowed to add another key-value pair?
In the final line of your script.py
file, in which you are attempting a lookup, are you trying to use the key or the value to perform that lookup?
Edited on October 21, 2018 to add the following …
This is your retrieve
method as it is currently coded …
def retrieve(self, key):
array_index = self.compress(self.hash(key))
list_at_index = self.array[array_index]
if list_at_index != None:
for pair in list_at_index:
if pair[0] == key:
return pair[1]
else:
return None
else:
return None
Consider that the problem may be in there, rather than in the .assign()
method.
Revisit instruction 19, which begins with …
Now we’re going to update
.retrieve()
to use separate chaining.
Begin by reducing that method to the following, then rebuild it by following the instructions, being careful to indent all the conditional blocks correctly …
def retrieve(self, key):
array_index = self.compress(self.hash(key))
I’ve spent soo much time on that and still no idea where the problem is…
I still get “None” when trying to retrieve a value from the newly added pair.
Oh, Gooood. I found it. I don’t know however why it did worked.
I deleted the first:
else:
return None
from
if list_at_index != None:
for pair in list_at_index:
if pair[0] == key:
return pair[1]
else:
return None
else:
return None
The variable, list_at_index
, represents the LinkedList
where key
should be found, if it is in the database. It might be at the first location in that LinkedList
, therefore we need to search the entire LinkedList
before we conclude that it is not in the database.
The else
block that you removed was causing the function to return
if key
was not within the first pair
in the LinkedList
. So if the key
, for example, was in the second pair
or later, it would not be found at all.