2013-03-08 63 views
0

您好我是python和编程的新手,我已经为wordgame编写了一些代码,当它运行时, 有一个'None'在输出之前打印出来。有没有办法删除它们,我知道它与循环无关,但我宁愿不必更改代码很多(如果可能的话)(第一次花了足够长的时间:)) 在此先感谢。在python的打印语句之前摆脱'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." 

回答

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

谢谢!那之后我觉得非常愚蠢。 – 2013-03-08 23:22:45

+0

@PadraicCunningham如果这回答您的问题,请随时通过点击绿色选中标记将其选为正确答案。 – askewchan 2013-03-08 23:27:26