私が書いたこのプログラムをデバッグしようとしています。与えられた単語,手およびword_listの場合、TrueかFalseを返しますか?変数のエラーの初期化を試してみて、それを修正して値を印刷しました。それは印刷されていないので、想定どおりに動作しているかわかりません。どんな助けもありがとうございます。私の関数がPythonでどのような値を返しているのかをどのように知ることができますか?
私は関数を持っていますload_words()これは単語のリストを返します。私はがWORD_LIST(私がチェックした)、これだけ単語は、辞書内のキーから完全に文字で構成されているかどうかを確認しようとし、この場合にはそれがない手、それがそうである単語を知っていますFalseを返します。また
、.keys差が(何)と.iterrkeys()、そしておそらく手紙、hand.iteritemsの値()と、手をループのより良い方法はありますか?
word = 'axel'
hand2 = {'b':1, 'x':2, 'l':3, 'e':1}
def is_valid_word(word, hand, word_list):
"""
Returns True if word is in the word_list and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or word_list.
word: string
hand: dictionary (string -> int)
word_list: list of lowercase strings
"""
failure = False
if word in word_list:
print hand
print [list(i) for i in word.split('\n')][0]
for letter in [list(i) for i in word.split('\n')][0]:
print letter
if letter in hand.keys():
print letter
return True
failure = True
print failure
else:
return False
failure = False
print failure
else:
return False
failure = False
print failure
is_valid_word(word,hand2,load_words())
UPDATE私は私の機能で、この機能を使用したいが、それはそれは自分自身で正常に動作していても、キーエラーになります。
def update_hand(hand, word):
"""
Assumes that 'hand' has all the letters in word.
In other words, this assumes that however many times
a letter appears in 'word', 'hand' has at least as
many of that letter in it.
Updates the hand: uses up the letters in the given word
and returns the new hand, without those letters in it.
Has no side effects: does not modify hand.
word: string
hand: dictionary (string -> int)
returns: dictionary (string -> int)
"""
for letter in [list(i) for i in word.split('\n')][0]:
if letter in hand.keys():
hand[letter] = hand[letter]-1
if hand[letter] <= 0:
del hand[letter]
display_hand(hand)
return hand
もちろん!素人間違い: – sampy
@sampyあなたのすべての質問に答えたので、私の最近の編集を見てください。それだけであれば、私の答えを受け入れることができます。ありがとう! –
これは優れています。なぜ私が1通の手紙しか印刷していないのか不思議に思っていました。はい、私はスクラブルゲームを作っています。この機能は、単語が有効であることを確認することです。私は手で単語の各文字を取り除く* update_hand *と呼ばれる関数を追加しようとしました。しかし、それは私の関数でそれを使用しようとすると、私はキーエラーを取得しますが、それは自分の上で正常に動作します。あなたの関数はまた、私はそれにテストプログラムを実行するときにのみ、キーエラーを発生させます。私はそれを含めるために私の質問を編集する、おそらくあなたはいくつかの洞察力を提供することができます。 – sampy