2014-09-11 190 views
-1

我是自學自學 Python通過麻省理工學院的OCW獲得6.00。 (所以,請不要評論「你不應該問作業問題」......我甚至不在麻省理工學院,就像我喜歡的那樣)。我目前被卡在Problem #3 in Problem Set #5功能導入不正確

這裏是ps5.py的(相關)部分:

def update_hand(hand, word): 
    """ 
    Uses up all the letters in the given word and returns 
    the new hand. Does not modify hand. 

    word: string 
    hand: dictionary (string -> int) 
    """ 
    hand = hand.copy() 
    for char in word: 
     hand[char] = hand.get(char,0)-1 
    return hand 

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 
    after = update_hand(hand.copy(),word) 
    for char in after: 
     if after[char] < 0: 
      return False 
    return True 

我跑的代碼,並返回正確的結果。

Loading word list from file... 
    83667 words loaded. 
play_game not implemented. 
play_hand not implemented. 
>>> word = "python" 
>>> hand = {'h':1,'n':1,'o':1,'p':1,'t':1,'y':1} 
>>> word_list = load_words() 
Loading word list from file... 
    83667 words loaded. 
>>> is_valid_word(word, hand, word_list) 
True 
>>> word = "cobra" 
>>> is_valid_word(word, hand, word_list) 
False 
>>> hand 
{'h': 1,'n': 1,'o': 1,'p': 1,'t': 1,'y': 1} 

我的問題是,當is_valid_word函數被導入到test_ps5.py,它似乎只返回False的一切,這意味着它失敗的測試案例的一半。

這裏是test_ps5.py的(相關)部分:

from ps5 import * 

def test_is_valid_word(word_list): 
    """ 
    Unit test for is_valid_word 
    """ 
    failure=False 
    # test 1 
    word = "hello" 
    hand = {'h':1, 'e':1, 'l':2, 'o':1} 
    if not is_valid_word(word, hand, word_list): 
     print "FAILURE: test_is_valid_word()" 
     print "\tExpected True, but got False for word: '" + word + "' and hand:", hand 
     failure = True 
    # test 2 passes 
    # test 3 
    hand = {'n': 1, 'h': 1, 'o': 1, 'y': 1, 'd':1, 'w':1, 'e': 2} 
    word = "honey" 
    if not is_valid_word(word, hand, word_list): 
     print "FAILURE: test_is_valid_word()" 
     print "\tExpected True, but got False for word: '"+ word +"' and hand:", hand 
     failure = True 
    # test 4 passes 
    # test 5 
    hand = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2} 
    word = "evil" 
    if not is_valid_word(word, hand, word_list): 
     print "FAILURE: test_is_valid_word()" 
     print "\tExpected True, but got False for word: '" + word + "' and hand:", hand 
     failure = True 
    # test 6 passes 
    if not failure: 
     print "SUCCESS: test_is_valid_word()" 

word_list = load_words() 

而這裏的結果,當我運行代碼:

Loading word list from file... 
    83667 words loaded. 
---------------------------------------------------------------------- 
Testing get_word_score... 
SUCCESS: test_get_word_score() 
---------------------------------------------------------------------- 
Testing update_hand... 
SUCCESS: test_update_hand() 
---------------------------------------------------------------------- 
Testing is_valid_word... 
FAILURE: test_is_valid_word() 
    Expected True, but got False for word: 'hello' and hand: {'h': 1, 'e': 1, 'l': 2, 'o': 1} 
FAILURE: test_is_valid_word() 
    Expected True, but got False for word: 'honey' and hand: {'e': 2, 'd': 1, 'h': 1, 'o': 1, 'n': 1, 'w': 1, 'y': 1} 
FAILURE: test_is_valid_word() 
    Expected True, but got False for word: 'evil' and hand: {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} 
---------------------------------------------------------------------- 
All done! 

我不明白什麼是造成問題的位置和原因。

+1

ps5.py是你寫的代碼嗎?也許可以在循環中加入一些打印語句來幫助調試究竟發生了什麼以及哪裏出錯? – audiodude 2014-09-11 03:29:26

+1

您是否用它實際使用的測試向量來試用它? – 2014-09-11 03:31:21

+0

@ IgnacioVazquez-Abrams - 是的,我試過每一個測試向量。他們像在'ps5.py'中那樣工作。 – 2012ssohn 2014-09-11 03:34:12

回答

0

問題不在於您發佈的代碼中。這工作得很好:

ps5.py

def update_hand(hand, word): 
    """ 
    Uses up all the letters in the given word and returns 
    the new hand. Does not modify hand. 

    word: string 
    hand: dictionary (string -> int) 
    """ 
    hand = hand.copy() 
    for char in word: 
     hand[char] = hand.get(char,0)-1 
    return hand 

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 
    after = update_hand(hand.copy(),word) 
    for char in after: 
     if after[char] < 0: 
      return False 
    return True 

words.py

#!/usr/bin/env python 

from ps5 import * 

def test_is_valid_word(word_list): 
    """ 
    Unit test for is_valid_word 
    """ 
    failure=False 
    # test 1 
    word = "hello" 
    hand = {'h':1, 'e':1, 'l':2, 'o':1} 
    if not is_valid_word(word, hand, word_list): 
     print "FAILURE: test_is_valid_word()" 
     print "\tExpected True, but got False for word: '" + word + "' and hand:", hand 
     failure = True 
    # test 2 passes 
    # test 3 
    hand = {'n': 1, 'h': 1, 'o': 1, 'y': 1, 'd':1, 'w':1, 'e': 2} 
    word = "honey" 
    if not is_valid_word(word, hand, word_list): 
     print "FAILURE: test_is_valid_word()" 
     print "\tExpected True, but got False for word: '"+ word +"' and hand:", hand 
     failure = True 
    # test 4 passes 
    # test 5 
    hand = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2} 
    word = "evil" 
    if not is_valid_word(word, hand, word_list): 
     print "FAILURE: test_is_valid_word()" 
     print "\tExpected True, but got False for word: '" + word + "' and hand:", hand 
     failure = True 
    # test 6 passes 
    if not failure: 
     print "SUCCESS: test_is_valid_word()" 


def main(): 
    wordlist = ['honey', 'evil', 'python', 'dogs', 'weasels', 'hello'] 
    test_is_valid_word(wordlist) 

if __name__ == '__main__': 
    main() 

在同一目錄下兩個文件,這裏的輸出:

[email protected]:~/Documents/src/sandbox$ ./words.py 
SUCCESS: test_is_valid_word() 
[email protected]:~/Documents/src/sandbox$ 

你暗示數倍你會留下很多「不相關」的代碼。除了從文件中加載單詞列表外,這個問題似乎沒有任何代碼需要。該問題必須位於您未顯示的代碼中。由於這個問題似乎並不需要,所以我建議或者(a)全部擺脫; (b)一步一步地去除它,直到你的問題得到解決,然後逐步增加回來並定期測試,這樣你就可以準確地看到代碼的哪一點會導致它崩潰;或者(c)只需在當前代碼中的戰略位置插入大量print語句,以便您可以確切地看到它失敗的位置和原因,這將爲您提供有關正在發生的事情的線索,並幫助您確定哪些代碼造成的問題。

你也不會告訴你如何叫test_is_valid_word(),所以它可能是一個簡單的問題。

from something import *通常也是一個壞主意,有時會導致命名空間污染導致的神祕問題。改爲嘗試from ps5 import update_hand, is_valid_word,看看是否有幫助。

沒有代碼的其餘部分(這又一次看起來不相關),你是唯一能夠做這些事情的人。