関数の実装に問題があります。
目的は、単語内にある場合、辞書ハンド内のキーの値を減らすことです。例えば:
word = hi
hand = {'h':2,'i':1}
-> 関数 update_hand(単語,手)
hand = {'h'1}
だから私は試しました:
def update_hand(hand, word):
for letter in range(len(word)):
if hand.get(word[letter],0) != 0:
hand[word[letter]] -= 1
if hand.get(word[letter],0) == 0:
del hand[word[letter]]
return hand
しかし、私がそれを呼び出すと、次のようになります:
Traceback (most recent call last):
File "/home/phillip/Desktop/ps3/ps3/ps3a.py", line 168, in <module>
print update_hand('quali', {'a': 1, 'i': 1, 'm': 1, 'l': 2, 'q': 1, 'u': 1})
File "/home/phillip/Desktop/ps3/ps3/ps3a.py", line 162, in update_hand
if hand.get(word[letter],0) != 0:
AttributeError: 'str' object has no attribute 'get'
だから私はそれをテストファイルに実装しようとしました(for戦利品だけ)、すべてうまくいきました...まあ、何が間違っていたのかわかりません。
ありがとう、フィリップ