我是编码新手,我无法让这个功能正常工作。
def isValidWord(word, hand, wordList):
"""
Returns True if word is in the wordList and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
wordDic = {}
if word not in wordList:
return False
for letter in word:
if letter in wordDic:
wordDic[letter] += 1
else:
wordDic[letter] = 1
if wordDic[letter] > hand[letter]: #
return False
return True
我想要做的是与字典值比较一个字母在 wordDic 中出现的次数以及它在手中出现的次数。但我不断收到“TypeError:列表索引必须是整数,而不是 str”。有人可以解释我哪里出错了吗?