2016-06-26 7 views
0

私が書いたこのプログラムをデバッグしようとしています。与えられた単語,および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 

答えて

4

あなたはそれprint秒前に機能を戻ってきているので、それがプリントアウトされていない理由はあります。これは、プログラムがprintステートメントに達する前に停止することを意味します。たとえば:

def foo(x): 
    print x 
    return x 

foo("asdf") 

ウィルprint

asdf 

だから、すべてあなたの文return

def foo(x): 
    return x 
    print x 

foo("asdf") 

はしばらく何も返さないだろう。そうでない場合、実行されません。 2番目の明確化のために

、この記事では、すでにあなたの答えhttps://stackoverflow.com/a/3617008を持っている:彼らは同じように動作しますが、Pythonの2、iter(d.keys())

d.iterkeys()は、非常に同等ではありません。最初に、keys()はディクショナリのキーリストのコピーを返し、iterはこのリストにイテレータオブジェクトを返します。次に、キーの完全なリストのコピーは作成されません。

Python 3には.iterkeys()もありません。 Python 3は前の.iterkeys()を新しい.keys()として使います。

最後にであり、コードには一般的に間違っているものと、重大度の高い順に達成したいものについて検討します。

  1. あなたのコードだけ一文字
  2. [list(i) for i in word.split('\n')][0]はあなたが単語のすべての文字を取得する方法ではありませんがチェックされます。
  3. 大きなインデントブロックを持たないように、最初に短いコードを戻す必要があります。

あなたのコードだけで、あなたのループのために、あなたが最初の単語がチェックされた直後にreturn Trueでは、1つの文字

をチェックします。代わりにループが完了したらreturn Trueする必要があります。

for letter in word: 
    if letter not in hand.keys(): 
     return False 
return True 

リスト内包

あなたのリストの内包は必要ありません(私はなぜ後であなたを教えてあげましょう)だけの単語からの手紙を取得するには非常に複雑である必要はありません。例えば。

for letter in word: 
    # code... 
:(私は上記したよう)

list(word) 

実際には、あなただけの、それは文字一つ一つ戻ります直接言葉を反復処理する必要があります

[list(i) for i in word.split('\n')][0] 

は実際にこれだけはありません

最初に短いコードを返します。

通常、非常にインデントされたコードの大きなチャンクは嫌です。あなたができることは、短いコードを最初に戻すことです。たとえば:

if word in word_list: 
    for letter in word: 
     if letter in hand.keys(): 
      return True 
     else: 
      return False 
else: 
    return False 

は、単に書くことができます:

if word not in word_list: 
    return False 

for letter in word: 
    if letter in hand.keys(): 
     return True 
    else: 
     return False 

しかし、これはちょうど私の意見です。他の人は、コードがいつ実行されるかを知るためにelse文を好むかもしれません。

def is_valid_word(word, hand, word_list): 
    if word not in word_list: 
     return False 

    for letter in word: 
     if letter not in hand.keys(): 
      return False 
    return True 

クリーン右:

あなたの最終的なコードは次のようになりますか?しかし、私はあなたがスクラブルゲームのようなものを作っていることを前提としているので、あなたが選んだ単語のためにあなたのhandの単語ができるかどうかを数えます。単語の文字の数がより少ないか、あなたの手で文字の数と等しい場合、何を追加できることはカウントするものです:

def is_valid_word(word, hand, word_list): 
    if word not in word_list: 
     return False 
    # This makes the word into a "unique list" 
    letters = set(word) 
    for letter in letters: 
     if hand[letter] < word.count(letter): 
      return False 

    return True 

EDIT コードに問題がありました。 if文でletterhandにあるかどうかはチェックされません。if hand[letter] < word.count(letter):

def is_valid_word(word, hand, word_list): 
    if word not in word_list and word not in hand.keys(): 
     return False 
    letters = set(word) 
    for letter in letters: 
     # Add this extra clause 
     if letter in hand.keys() or hand[letter] < word.count(letter): 
      return False 

    return True 
+0

もちろん!素人間違い: – sampy

+0

@sampyあなたのすべての質問に答えたので、私の最近の編集を見てください。それだけであれば、私の答えを受け入れることができます。ありがとう! –

+0

これは優れています。なぜ私が1通の手紙しか印刷していないのか不思議に思っていました。はい、私はスクラブルゲームを作っています。この機能は、単語が有効であることを確認することです。私は手で単語の各文字を取り除く* update_hand *と呼ばれる関数を追加しようとしました。しかし、それは私の関数でそれを使用しようとすると、私はキーエラーを取得しますが、それは自分の上で正常に動作します。あなたの関数はまた、私はそれにテストプログラムを実行するときにのみ、キーエラーを発生させます。私はそれを含めるために私の質問を編集する、おそらくあなたはいくつかの洞察力を提供することができます。 – sampy

1

あなたがreturn文は無益である直接print is_valid_word(word,hand2,load_words())

0

あなたは、いくつかのインデントの問題を持っている、と後に何かをやって結果を印刷することができます。

あなたはinオペレータがあなたのためにチェックし、リスト、セット、dicts(キー)、タプル、文字列、で動作しますkeysまたはiterkeysを使用する必要はありません...

inオペレータ呼び出しますほとんどのPythonコレクションでサポートされている__contains__です。

​​もご覧ください。

彼は、3回のテストで何をしたいかを最小限に抑えた例です。

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 

    """ 

    if word not in word_list: 
     return False 

    for letter in word: 
     if letter not in hand: 
      return False 

    return True 

print(is_valid_word('bxel', 
        {'b': 1, 'x': 2, 'l': 3, 'e': 1}, 
        ['foo', 'bar', 'bxel'])) 
print(is_valid_word('axel', 
        {'b': 1, 'x': 2, 'l': 3, 'e': 1}, 
        ['foo', 'bar', 'axel'])) 
print(is_valid_word('axel', 
        {'a': 1, 'x': 2, 'l': 3, 'e': 1}, 
        ['foo', 'bar', 'axel'])) 
関連する問題