功能导入不正确

问题描述:

我是自学自学 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

问题不在于您发布的代码中。这工作得很好:

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,看看是否有帮助。

没有代码的其余部分(这又一次看起来不相关),你是唯一能够做这些事情的人。