2016-10-17 7 views
1

스크래블 타입 게임을 구현하려고하는데 playHand가 AttributeError를 반환합니다. 'str'객체에 'copy'속성이 없습니다. 이 오류는 updateHand Hand = Hand.copy()에서 발생하지만 개별적으로 updateHand를 테스트했으며 해당 ow에 대해 정상적으로 작동합니다. 나는 그것을 찾아 보았지만 아무 것도 발견하지 못했습니다. 길이에 사과! 나는 그것을 도울 수 없다. 감사!문자열이 사전에 돌연변이되었지만 어디에 있는지 알 수 없습니다.

VOWELS = 'aeiou' 
CONSONANTS = 'bcdfghjklmnpqrstvwxyz' 
HAND_SIZE = 7 

SCRABBLE_LETTER_VALUES = { 
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10 
} 

def getWordScore(word, n): 
""" 
Returns the score for a word. Assumes the word is a valid word. 

The score for a word is the sum of the points for letters in the 
word, multiplied by the length of the word, PLUS 50 points if all n 
letters are used on the first turn. 

Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is 
worth 3, D is worth 2, E is worth 1, and so on (see SCRABBLE_LETTER_VALUES) 

word: string (lowercase letters) 
n: integer (HAND_SIZE; i.e., hand size required for additional points) 
returns: int >= 0 
""" 
score = 0 

for letter in word: 
    score += SCRABBLE_LETTER_VALUES[letter] 

return score * len(word) + 50 * (len(word) == n) 

def displayHand(hand): 
""" 
Displays the letters currently in the hand. 

For example: 
>>> displayHand({'a':1, 'x':2, 'l':3, 'e':1}) 
Should print out something like: 
    a x x l l l e 
The order of the letters is unimportant. 

hand: dictionary (string -> int) 
""" 
for letter in hand.keys(): 
    for j in range(hand[letter]): 
     print(letter,end=" ")  # print all on the same line 
print()        # print an empty line 


def updateHand(hand, word): 
""" 
Assumes that 'hand' has all the letters in word. 
In other words, this assumes that however many times 
a letter appears in 'word', 'hand' has at least as 
many of that letter in it. 

Updates the hand: uses up the letters in the given word 
and returns the new hand, without those letters in it. 

Has no side effects: does not modify hand. 

word: string 
hand: dictionary (string -> int)  
returns: dictionary (string -> int) 
""" 

Hand = hand.copy() 

for letter in word: 
    Hand[letter] -= 1 

return Hand 



def isValidWord(word, hand, wordList): 
""" 
Returns True if word is in the wordList and is entirely 
composed of letters in the hand. Otherwise, returns False. 

Does not mutate hand or wordList. 

word: string 
hand: dictionary (string -> int) 
wordList: list of lowercase strings 
""" 
Hand = hand.copy() 

if word not in wordList: 
    return False 

for letter in word: 
    if letter not in Hand: 
     return(False) 
     break 
    else: 
     Hand[letter] -= 1 
     if Hand[letter] < 0: 
      return False 
return True  

def calculateHandlen(hand): 
""" 
Returns the length (number of letters) in the current hand. 

hand: dictionary (string int) 
returns: integer 
""" 
return sum(hand.values()) 

def printCurrentHand(hand): 
print('Current Hand: ', end = ' ') 
displayHand(hand) 

def printTotalScore(score): 
print('Total: ' + str(score) + ' points') 

def updateAndPrintScore(score, word, n): 
currentScore = getWordScore(word, n) 
score += currentScore 
print('" ' + word + ' "' + ' earned ' + str(currentScore) + ' points.', end    = ' ') 
print(printTotalScore(score))  

return score 


def playHand(hand, wordList, n): 
""" 
Allows the user to play the given hand, as follows: 

* The hand is displayed. 
* The user may input a word or a single period (the string ".") 
    to indicate they're done playing 
* Invalid words are rejected, and a message is displayed asking 
    the user to choose another word until they enter a valid word or "." 
* 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 or the user 
    inputs a "." 

    hand: dictionary (string -> int) 
    wordList: list of lowercase strings 
    n: integer (HAND_SIZE; i.e., hand size required for additional points) 

""" 
score = 0# Keep track of the total score 
hand_copy = hand.copy() 

while calculateHandlen(hand_copy) > 0: 
    printCurrentHand(hand_copy) 
    word = input("Enter word, or a \".\" to indicate that you are finished: ") 

    if word == '.': 
     print("Goodbye!", end = ' ') 
     break 

    if isValidWord(word, hand_copy, wordList): 
     score += updateAndPrintScore(score, word, n) 
     hand_copy = updateHand(word, hand_copy) 

    else: 
     print('Invalid word!') 
     print() 

if calculateHandlen(hand_copy) == 0: 
    print("Run out of letters.", end = ' ') 

printTotalScore(score)  

playHand({'h':1, 'i':1, 'c':1, 'z':1, 'm':2, 'a':1},['him', 'cam'], 7) 

오류 코드

runfile('C:/Users/.spyder2-py3/ProjectsMIT/temp.py', wdir='C:/Users/.spyder2-py3/ProjectsMIT') 
Current Hand: c a h i m m z 

Enter word, or a "." to indicate that you are finished: him 
" him " earned 24 points. Total: 24 points 
None 
Traceback (most recent call last): 

    File "<ipython-input-2-2e4e8585ae85>", line 1, in <module> 
    runfile('C:/Users/.spyder2-py3/ProjectsMIT/temp.py', wdir='C:/Users/.spyder2-py3/ProjectsMIT') 

    File "C:\Users\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile 
    execfile(filename, namespace) 

    File "C:\Users\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    line 183, in <module> 
    playHand({'h':1, 'i':1, 'c':1, 'z':1, 'm':2, 'a':1},['him', 'cam'], 7) 

    line 172, in playHand 
    hand_copy = updateHand(word, hand_copy) 

    line 72, in updateHand 
    Hand = hand.copy() 

AttributeError: 'str' object has no attribute 'copy' 
+1

정확한 추적을 포함하여 정확한 오류 메시지를 포함하도록 질문을 편집하십시오. –

+0

알림을 보내 주셔서 감사합니다. 완료 – MoRe

답변

2

귀하 전달 된 인수 updateHand()에 예상되는 인수에서 반전됩니다

라인 (161) :

hand_copy = updateHand(word, hand_copy) 

행 49 :

def updateHand(hand, word): 

updateHand은 손이 첫 번째 arg가 될 것으로 예상하지만 두 번째 arg는 손을 넘깁니다.

수정 사항은 간단합니다. 라인 161을 다음으로 바꾸십시오 :

hand_copy = updateHand(hand_copy, word) 
+0

정말 고마워요! – MoRe

+0

후손을 위해 단어가 손으로 해석 되었습니까? – MoRe