我是自学自学 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!
我不明白什么是造成问题的位置和原因。
ps5.py是你写的代码吗?也许可以在循环中加入一些打印语句来帮助调试究竟发生了什么以及哪里出错? – audiodude 2014-09-11 03:29:26
您是否用它实际使用的测试向量来试用它? – 2014-09-11 03:31:21
@ IgnacioVazquez-Abrams - 是的,我试过每一个测试向量。他们像在'ps5.py'中那样工作。 – 2012ssohn 2014-09-11 03:34:12