I have a problem with the implementation of a function.
The aim is to decrease the value of a key in the dictionary hand if it is in the word. For example:
word = hi
hand = {'h':2,'i':1}
-> function update_hand(word,hand)
hand = {'h'1}
so I tried:
def update_hand(hand, word):
for letter in range(len(word)):
if hand.get(word[letter],0) != 0:
hand[word[letter]] -= 1
if hand.get(word[letter],0) == 0:
del hand[word[letter]]
return hand
but when I call it, I get:
Traceback (most recent call last):
File "/home/phillip/Desktop/ps3/ps3/ps3a.py", line 168, in <module>
print update_hand('quali', {'a': 1, 'i': 1, 'm': 1, 'l': 2, 'q': 1, 'u': 1})
File "/home/phillip/Desktop/ps3/ps3/ps3a.py", line 162, in update_hand
if hand.get(word[letter],0) != 0:
AttributeError: 'str' object has no attribute 'get'
So I tried to implement it in a test file (just the for loot) and everything works fine... well, I have no idea what I did wrong.
Thanks, Phillip