level: beginner
word= 'even'
dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}
i want to know if word is entirely composed of letters in dict2 my approach:
step 1 : convert word to dictionary(dict1)
step2:
for k in dict1.keys():
if k in dict2:
if dict1[k] != dict2[k]:
return False
return True
by adding a print statement i can see that this simply ends too early e.g. as soon as the first IF condition is met the loop exits and i won't get a correct answer. i think this is easy but google and python doc didn't return any good hints so i'm trying here.
Thanks Baba
UPDATE
the number of times that each letter ocurs in the word needs to be smaller or equal to the number of times it apears in dict2. That way i am guaranteed that word is entirely made up of elements of dict2.
for k in word.keys(): # word has ben converted to key already
if k not in hand:
return False
elif k in hand:
if word[k] > hand[k]:
return False
return True