随机洗牌错误消息

2024-06-25 23:50:04 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在写一个垄断游戏模拟器,并有以下列表 卡对象内的公益金卡号:

self.CChcards_MessNo = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

我想用下面的方法洗牌

^{pr2}$

它在程序的早期起作用,但失败并给出 下面的消息在程序的主要部分后面。在

  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张牌,现在需要洗牌时,就会发生这种情况


Tags: 对象方法inself程序游戏消息列表
1条回答
网友
1楼 · 发布于 2024-06-25 23:50:04
>>> 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()

相关问题 更多 >