为什么我会收到这个错误提示 "TypeError: 'method' object is not iterable"?

8

我不明白为什么列表不能迭代。我们初始化了这个列表,并在其中添加了拼图的各个部分。

用户需要输入他们想要的棋盘行数和列数。每一行是一个新的拼图部分,每一列也是一个新的拼图部分。每个拼图部分都是一个由3个列表组成的矩阵。我的想法是创建一个pieces=[]列表,并将每个新生成的拼图部分附加到该列表中。

我的问题是,我无法让str()打印出整个棋盘。我想我们应该打印pieces列表中每个拼图部分的三个列表中的第一个列表,然后是第二个列表,最后是第三个列表。并且在一个新行开始。虽然我不知道如何实施它。

或者我可以先单独打印每个拼图部分,然后将这些拼图部分添加到字符串中以打印整个棋盘。所以我将不使用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())

这是我得到的错误信息:

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

1
请发布一个 MCVE 而不是整个代码。此外,始终包括 traceback,以便我们至少可以查看问题发生的位置。虽然从错误中应该很清楚你实际上没有在列表上进行迭代。运行调试器并找出为什么会崩溃。 - Vincent Savard
1个回答

11

你误解了错误信息:它并不是说列表不能进行迭代,而是说方法不能进行迭代。

问题在于你的 pieces 列表并不包含棋子,而是引用了 add_piece 方法,因为你在第56行中想要追加它的结果时忘记调用该方法。

即使你对异常类型的经验较少,也可以通过在引发错误的那一行之前调用调试器 (pdb) 来找到此错误。


所以我删除了__rpr()__方法,想看看它通过str()方法的效果,但它并没有按照我想要的方式打印。你有什么想法可以解决这个问题吗? - Jessy White
1
你需要调用 add_piece。是 add_piece(),不是 add_piece。 - Max
谢谢你打印了列表!但是当我运行Jigsaw(3,5)而没有调用puzzleboard时,即使我在方法中将print更改为return,它也不会通过str方法。它什么都不返回。 - Jessy White

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接