A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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.

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks for the tip :)

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.