-1

I have a function that yields different results compared to what they are supposed to yield . This is for a scrabble game in python. Here's the method - the functions and their various types:

num = 0
length = (int(len(hand))/2) - 1
print('Current Hand: '),
print(displayHand(hand))

while (len(hand)>0):


    lettersGuessed = raw_input('Enter word, or a "." to indicate that you are finished: ')
    word= lettersGuessed

    if (isValidWord(word, hand, wordList)==False):
        print("Invalid word, please try again."+'\n')
    if (isValidWord(word, hand, wordList)==True):
        num += getWordScore(word, n)
        length -= 1
        updateHand(hand, word)
        print('"'+word+'"'+" earned " +str(getWordScore(word, n))+" points. "+" Total: "+str(num)+'\n')
        print('Current Hand: '),
        print(displayHand(hand))

    if (isValidWord(word, hand, wordList)==True):
        num += getWordScore(word, n)
        length -= 1
        updateHand(hand, word)
        print('"'+word+'"'+" earned " +str(getWordScore(word, n))+" points. "+" Total: "+str(num)+'\n')
        if length>=1: 
            print('Current Hand: '),
            print(displayHand(hand))
    if (isValidWord(word, hand, wordList)==True):
        num += getWordScore(word, n)
        length -= 1
        updateHand(hand, word)
        print('"'+word+'"'+" earned " +str(getWordScore(word, n))+" points. "+" Total: "+str(num)+'\n')

         #   print('Current Hand: '),
          #  print(displayHand(hand))
        if (length==0)&(isValidWord(word, hand, wordList)==False):
            print("Run out of letters. Total score: "+str(num))

    if (length==0):
        print("Run out of letters. Total score: "+str(num))
        break;
    if (length==0)&(isValidWord(word, hand, wordList)==True):
        print("Run out of letters. Total score: "+str(num))
        break;
    if (lettersGuessed =="."):
        print("Goodbye! Total score: "+str(num))
        break;

Here are the test cases that were done. Here's a test case that is supposed to result from the above output but it doesn't.

Can anyone please see what is wrong with my code?

Function Call:

wordList = loadWords()
playHand({'w':1, 's':1, 't':2, 'a':1, 'o':1, 'f':1}, wordList, 7)
Output:
  Current Hand:  a s t t w f o
  Enter word, or a "." to indicate that you are finished: tow
  "tow" earned 18 points. Total: 18 points

  Current Hand:  a s t f
  Enter word, or a "." to indicate that you are finished: tasf
  Invalid word, please try again.

  Current Hand:  a s t f
  Enter word, or a "." to indicate that you are finished: fast
  "fast" earned 28 points. Total: 46 points. 

  Run out of letters. Total score: 46 points. 

Instead of it saying Run out of letters. Total score: 46. it says the following:

Current Hand: 
None
Run out of letters. Total score: 46

how do I remove the code:

Current Hand:
None

Can someone please copy my code and put in the letters in the hands for the various test cases please?

4

1 回答 1

0

问:

如何删除代码:当前手牌:无

A: 在第三行,你打印:Current Hand:不管你有没有手。使该代码有条件(就像您在其他地方一样)您的手是否None存在。

或使用:

if length>=1 and hand is not None: 
        print('Current Hand: '),
        print(displayHand(hand))
于 2013-03-10T12:07:55.660 回答