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