代码中不必要的部分 - " wordlist加载"在末尾

时间:2014-06-01 05:00:39

标签: python

只需快速检查即可。我正在进行文字游戏,想要找到给定手牌的点数。我已经获得了大部分代码,并且运行正常。但是,我一直在底部重复一部分代码,我想知道如何删除它?这是代码:

def play_hand(hand, wordlist):

    """
    Allows the user to play the given hand, as follows:

    * The hand is displayed.

    * The user may input a word.

    * An invalid word is rejected, and a message is displayed asking
      the user to choose another word.

    * When a valid word is entered, it uses up letters from the hand.

    * After every valid word: the score for that word is displayed,
      the remaining letters in the hand are displayed, and the user
      is asked to input another word.

    * The sum of the word scores is displayed when the hand finishes.

    * The hand finishes when there are no more unused letters.
      The user can also finish playing the hand by inputing a single
      period (the string '.') instead of a word.

      hand: dictionary (string -> int)
      word_list: list of lowercase strings

    """
    print "Current Hand:", display_hand(hand)
    word = raw_input('Enter word, or a "." to indicate that you are finished:')
    handScore = 0

    while list(word) != ['.']:
        if is_valid_word(word, hand, wordlist) == True:
            score = get_word_score(word,n)
            handScore = handScore + score
            print word, "earned", score, "points. Total:", handScore, "points"
            hand = update_hand(hand, word)

            if calculate_handlen(hand) == 0:
                    print "Your hand has finished."
                    print "Your final score is", handScore
                    break
            else:
                print "Current hand:", display_hand(hand)
                word = raw_input('Enter word, or a "." to indicate that you are finished:')

        else:
            print "Invalid word, please try again."
            word = raw_input('Enter word, or a "." to indicate that you are finished:')


    if list(word) == ['.']:
        print "Total score: %d points" %(handScore)


n = 7
hand = deal_hand(n)
play_hand(hand, load_words())

以下是load_words()的代码:

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print "Loading word list from file..."
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # wordlist: list of strings
    wordlist = []
    for line in inFile:
        wordlist.append(line.strip().lower())
    print "  ", len(wordlist), "words loaded."
    return wordlist

我得到了回报:

Loading word list from file...
   83667 words loaded.
Current Hand: a i j m n w v
None
Enter word, or a "." to indicate that you are finished:jam
jam earned 36 points. Total: 36 points
Current hand: i n w v
None
Enter word, or a "." to indicate that you are finished:win
win earned 18 points. Total: 54 points
Current hand: v
None
Enter word, or a "." to indicate that you are finished:v
Invalid word, please try again.
Enter word, or a "." to indicate that you are finished:.
Total score: 54 points
Loading word list from file...
   83667 words loaded.

正如你所看到的,还有一个额外的“从文件加载单词列表...加载了83667个单词”。在底部,不应该在那里。有谁知道我的代码出了什么问题?

非常感谢! 安德烈

1 个答案:

答案 0 :(得分:1)

也许从一些函数中再次调用load_words()函数,这在函数中是看不到的。

如果删除play_hand()并在此处显示输出怎么办?

注意:您始终可以使用像pudb这样的调试器来检查程序流程。