为什么我会收到此错误"TypeError:'method'对象不可迭代"?

Jes*_*ite 3 python python-3.x

我不明白列表是如何无法迭代的.我们初始化了列表,并使用它在列表中添加了一个拼图游戏.

用户要输入他们想要的板的行数和列数.每一行都是一个新的部分,每一列都是一个新的部分.每个部分都是3个列表的列表,看起来像一个矩阵.我的想法是创建一个list pieces = []并将每个新生成的片段附加到列表中.

我的问题是我无法让str()打印出来.我想我们应该在片段列表中打印每个片段的3个列表的第一个列表,然后打印3个列表的中间列表,然后是三个列表的最后一个列表.从一个新的开始.我知道如何实现它.

或者我以为我可以单独打印每一件,然后在该字符串上添加碎片以打印出电路板.所以我不会使用pieces = []列表.我不知道哪个最好或如何实现它们.

我试着看其他问题,但我找不到答案,我尝试打印该类的字符串表示:

from random import randint
from pprint import pprint


class Jigsaw:

    def __init__(self, r, c):
        self.pieces = []
        self.row = r
        self.col = c

    def __str__(self):
        """
        Writes the state and value to a string.
        """
        s =" "
        d = ""
        f = ""
        g = ""
        for row in self.pieces:
        #print(row)
            for col in row:
               #print(col)
                d = d +" "+ format(col[0])
        print(d)
        for row in self.pieces:
            #print(row)
            for col in row:
            #print(col)
                f = f +" " + format(col[1])
        print(f)

        for row in self.pieces:
            #print(row)
            for col in row:
                #print(col)
                g = g +" "+ format(col[2])
        print(g)



    #     return pce
    # result = 'ball : %s' \
    #     % (pce)
        #return r

    def __repr__(self):
        """
        Returns the string representation.
        """
        return str(self)

    def puzzle_board(self):
        for c in range(self.row):
            for d in range(self.col):
                self.pieces.append(self.add_piece)

        return self.pieces

    def add_piece(self):
        a = PieceGenerator()
        b = self.a_piece(a.idn,a.top,a.left,a.right,a.bottom)
        self.pieces.append(b)
        return(b)


    def mis(self):
        self.add_piece()

     # def boardShuffle(self,board):

    def a_piece(self, id, top,left,right,bottom):
        """
        Returns the piece values.
        """
        pce = [[" ", top, " "], [left, id, right], [" ", bottom, " "]]
        return(pce)

    # def puzzleSolve(self,board):

class PieceGenerator:
    """
    Creates new puzzle pieces with their values.
    """
    idn = 0 #Global variable to keep track of all the pieces
    def __init__(self):
        """
        A piece have top, bottom, right, left values and puzzle piece it self
        """
        self.top = randint(10,99)
        self.bottom = randint(10,99)
        self.right = randint(10,99)
        self.left = randint(10,99)
        PieceGenerator.idn += 1

print(Jigsaw(3,5).puzzle_board())
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

Traceback (most recent call last):
   File "C:/Users/HP/PycharmProjects/untitled/.idea/jigsaw.py", line 102, in    <module>
    print(Jigsaw(3,5).puzzle_board())
   File "C:/Users/HP/PycharmProjects/untitled/.idea/jigsaw.py", line 56, in  __repr__
    return str(self)
   File "C:/Users/HP/PycharmProjects/untitled/.idea/jigsaw.py", line 22, in __str__
    for col in row:
TypeError: 'method' object is not iterable
Run Code Online (Sandbox Code Playgroud)

Tho*_*tze 7

你误解了错误信息:它没有说列表不可迭代,它说方法不是.

会发生的情况是,您的pieces列表不包含片段,而是对add_piece方法的引用,因为您想要在第56行附加其结果时忘记调用该方法.

通过pdb在引发错误的行之前调用调试器(),您甚至可以发现此错误,即使对异常类型的经验较少.