我是編碼方面的新手,我無法正確使用此功能。無法比較字典值
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」。有人可以解釋我出錯的地方嗎?
什麼是'手'?這很可能是一個列表,而不是一個字典。向我們展示處理「手」的代碼。 – 2013-03-08 13:49:18
哪一行產生錯誤? – thegrinner 2013-03-08 13:50:21
@thegrinner# – asheeshr 2013-03-08 13:51:23