谁能给我解释一下这个错误消息吗?
# 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
编辑: 我一路上又换了手,并用它创建了一个元组而不是字典。谢谢!
最佳答案
错误非常明显:您有一个元组,您尝试在其上调用copy
。
看来您手中的对象不是您想要的对象,而是一个元组。
关于python - AttributeError: 'tuple' 对象没有属性 'copy',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946556/