2017-06-20 88 views
-2

你好我寫一個大富翁遊戲模擬器,並有 公益金卡號的卡對象中的以下列表: -隨機洗牌錯誤信息

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:\Users\David\AppData\Local\Programs\Python\Python35\lib\random.py", line 278, in shuffle 
    for i in reversed(range(1, len(x))): 
TypeError: object of type 'int' has no len() 

當程序已經通過16卡循環,就會出現此,現在需要洗牌

+1

因爲'x'是一個整數... –

+5

有代碼(在你的問題中未示出),它給CChcards_MessNo分配一個整數。 – NPE

+1

我猜''self.CChcards_MessNo'在這一點上不再是一個列表。 –

回答

1
>>> 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-32\lib\random.py", line 271, in shuffle 
    for i in reversed(range(1, len(x))): 
TypeError: object of type 'int' has no len() 
+1

** TL; DR **:在您的代碼中的某處分配'self.CChcards_MessNo'。 –

+0

^被設置爲一個整數。 –

+0

是的,我已經設置爲一個整數! Thankyou所有的repliers。我被錯誤消息難住 - 大概與洗牌模塊中的一行有關。 – deezil