在Python打印语句之前去掉'NONE

1 投票
1 回答
2883 浏览
提问于 2025-04-17 18:28

你好,我是Python和编程的新手。我写了一些代码做一个单词游戏,但在运行的时候,有些输出前面会出现一个'None'。请问有没有办法去掉这个'None'?我知道这和循环没有返回值有关,但如果能不大改代码就好了(我第一次写代码花了很长时间 :))谢谢大家!

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."

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."

撰写回答