Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

PS5, problem 3... I'm so close! The problem I am having is when I call the update_hand function in is_valid_word I get an error when it's checking for # test 4 hand = {'r': 1, 'a': 3, 'p': 2, 't': 1, 'u':2} word = "honey" The criteria for update_hand explicitly states that we are to assume that 'hand' has all the letters in word. Therefore it passes when testing the update_hand function. If I delete this from the test_ps5.py file, it runs successfully.

OpenStudy (anonymous):

Here's my code for update_hand and is_valid_word. Any tips are greatly appreciated! def update_hand(hand, word): new_hand = hand for letter in word: new_hand[letter] = new_hand[letter] -1 return new_hand def is_valid_word(word, hand, word_list): test_hand = update_hand(hand, word) for letter in word: if test_hand.get(letter,0) < 0: return False match_found = False i = 0 while match_found == False and i < len(word_list): if word == word_list[i]: match_found = True break else: i += 1 return match_found

OpenStudy (anonymous):

Your version of is_valid_word is fine, your version of update_hand will raise KeyError when it's given a word that's not composable with the letters in the hand. You'll need to do something like the statement below to prevent the errors in the 'honey' test. new_hand[letter] = new_hand.get(letter,0) -1 Also your update_hand function mutates the hand it's given (which you don't want to do). Be sure to make a copy of it, don't just give the same dictionary a new name.

OpenStudy (anonymous):

Thanks for the tip :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!