嗨,我正在编写一个大富翁游戏模拟器,并有以下列表公益金卡对象内的号码:-
self.CChcards_MessNo = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
我想洗牌这些,用下面的方法
def shuffle(self):
import random
random.shuffle(self.CChcards_MessNo)
它在程序的早期工作,但失败并给出以下消息稍后在程序的主要部分。
File "C:UsersDavidAppDataLocalProgramsPythonPython35librandom.py", line 278, in shuffle
for i in reversed(range(1, len(x))):
TypeError: object of type 'int' has no len()
当程序循环浏览 16 张牌并且现在需要洗牌时,会发生这种情况
>>> class Foo():
... def __init__(self):
... self.CChcards_MessNo = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
... def shuffle(self):
... import random
... random.shuffle(self.CChcards_MessNo)
... def bug(self):
... print("I'm a bug that makes shuffle() fail by assigning an int to self.CChcards_MessNo")
... self.CChcards_MessNo = 0
...
>>> foo = Foo()
>>> foo.shuffle()
>>> foo.bug()
I'm a bug that makes shuffle() fail by assigning an int to self.CChcards_MessNo
>>> foo.shuffle()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in shuffle
File "C:Program Files (x86)Python36-32librandom.py", line 271, in shuffle
for i in reversed(range(1, len(x))):
TypeError: object of type 'int' has no len()