功能无法正确导入

时间:2014-09-11 03:17:55

标签: python

自学 Python通过麻省理工学院的OCW 6.00。 (所以请不要评论“你不应该要求做作业问题”......我甚至不在麻省理工学院,就像我希望的那样。)我现在被困在{ {3}}

以下是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 个答案:

答案 0 :(得分: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()

将两个文件放在同一目录中,输出结果为:

paul@local:~/Documents/src/sandbox$ ./words.py
SUCCESS: test_is_valid_word()
paul@local:~/Documents/src/sandbox$ 

你有几次暗示你遗漏了很多不相关的&#34;码。除了从文件加载单词列表之外,此问题似乎不需要该代码。问题必须存在于您未显示的代码中。由于这个问题似乎并不需要,我建议(a)全部摆脱它; (b)逐个摆脱它,直到你的问题得到解决,然后逐步添加并定期测试,这样你就可以确切地看到代码会导致它破坏;或者(c)在当前代码中的战略位置插入大量print语句,这样您就可以确切地知道它失败的地点和原因,这将为您提供有关进展情况的线索on并帮助您隔离导致问题的代码位。

你也没有显示你如何调用test_is_valid_word(),所以这可能是一个简单的问题。

from something import *通常也是一个坏主意,有时会导致名称空间污染导致的神秘问题。请尝试from ps5 import update_hand, is_valid_word,看看是否有帮助。

没有剩下的代码(无论如何,这似乎都不相关),你是唯一可以做这些事情的人。