0

Should Return True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False.

def is_valid_word(word, hand, word_list):
### word = string, hand = dictionary, word_list = string
   count = 0
   for letter in word:
       if letter in hand and word_list:
           count += 1
   if count == len(word):
       return True
   else:
       return False


is_valid_word('star', {'a': 0, 's': 3, 'i': 0, 'e': 1, 'l': 1, 't': 3,'r':1}, 'sctdahr') ##returns true
is_valid_word('star', {'i': 0, 'e': 1, 'l': 1, 't': 3,'r':1}, 'sctdahr') ###returns false
is_valid_word('star', {'a': 0, 's': 3, 'i': 0, 'e': 1, 'l': 1, 't': 3,'r':1}, 'sc') ###returns true

Why the last statement is returning True instead of False?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
HSRAO
  • 373
  • 1
  • 3
  • 4
  • `word_list` is always true because it is not empty. It is independent from the `letter in hand` expression on the other side of the `and` operator; Boolean expressions are not English sentences. – Martijn Pieters May 31 '15 at 11:11
  • It sounds like you actually want to 'return word in word_list and set(word).issubset(set(hand))' to me. In other words to ensure that each of the letters in "word" is found in the hand (its a subset) and that it's also among the word_list. Of course this assumes that you don't care about words with duplicated letters. – Jim Dennis May 31 '15 at 11:35

0 Answers0