1

嗨,我是 python 和编程的新手,我已经为文字游戏编写了一些代码,当它运行时,在某些输出之前会打印出“无”。有没有办法删除它们,我知道这与循环不返回任何内容有关,但如果可能的话,我宁愿不必大量更改代码(第一次花了我足够长的时间:))提前致谢。

def compPlayHand(hand, wordList, n):

    #  Keep track of the total score
    totalScore = 0
    # As long as there are still usable letters left in the hand:
    while compChooseWord(hand,wordList,n) is not None:

        # Display the hand

        print "Current Hand: ",
        print  displayHand(hand),

        word = compChooseWord(hand,wordList,n)  # comp chooses word
        hand = updateHand(hand,word)
        # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line
        getWordScore(word,n)
        totalScore += getWordScore(word,n)
        # Update the hand
        c = calculateHandlen(hand)

        print   '"'+str(word)+'"' + " earned " + str(getWordScore(word,n)) +' points.' " Total:  " + str(totalScore) + " points."     # Otherwise (the word is valid):
        print

        if compChooseWord(hand,wordList,n) is None:  # End the game (break out of the loop)

            print  "Current Hand: ", \
                displayHand(hand),
            print "Total score: " + str(totalScore) + " points."
4

1 回答 1

3

我们已经解决了这个问题,不要print displayHand,只是自己调用它。

def compPlayHand(hand, wordList, n):
    #  Keep track of the total score
    totalScore = 0
    # As long as there are still usable letters left in the hand:
    while compChooseWord(hand,wordList,n) is not None:

        # Display the hand

        print "Current Hand: ",
        displayHand(hand)

        word = compChooseWord(hand,wordList,n)  # comp chooses word
        hand = updateHand(hand,word)
        # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line
        getWordScore(word,n)
        totalScore += getWordScore(word,n)
        # Update the hand
        c = calculateHandlen(hand)

        print   '"'+str(word)+'"' + " earned " + str(getWordScore(word,n)) +' points.' " Total:  " + str(totalScore) + " points."     # Otherwise (the word is valid):
        print

        if compChooseWord(hand,wordList,n) is None:  # End the game (break out of the loop)

            print  "Current Hand: ",
            displayHand(hand)

            print "Total score: " + str(totalScore) + " points."
于 2013-03-08T23:16:10.993 回答