任何人都可以向我解释此错误消息吗?
# make a copy or hand is destroyed by your test
remaining = hand.copy()
结果:
AttributeError: 'tuple' object has no attribute 'copy'
我可以这样做:
remaining = copy.copy(hand)
返回:
(None, {hand...})
这会抛弃我正在尝试完成的内容,因为函数在搜索None返回值时返回False。
这是创建手的功能:
def deal_hand(n):
"""
Returns a random hand containing n lowercase letters.
At least n/3 the letters in the hand should be VOWELS.
Hands are represented as dictionaries. The keys are
letters and the values are the number of times the
particular letter is repeated in that hand.
n: int >= 0
returns: dictionary (string -> int)
"""
hand={}
num_vowels = n / 3
for i in range(num_vowels):
x = VOWELS[random.randrange(0,len(VOWELS))]
hand[x] = hand.get(x, 0) + 1
for i in range(num_vowels, n):
x = CONSONANTS[random.randrange(0,len(CONSONANTS))]
hand[x] = hand.get(x, 0) + 1
return hand
编辑: 我一路走得更远,用它创建了一个元组而不是字典。谢谢!
答案 0 :(得分:1)
错误非常明确:您有一个元组,您尝试调用copy
。
您hand
中的对象似乎不是您想要的对象,而是一个元组。