简体   繁体   English

为什么我会收到此错误“TypeError: 'method' object is not iterable”?

[英]Why do I get this error "TypeError: 'method' object is not iterable"?

I do not understand how a list can not be iterable.我不明白列表如何不能迭代。 We initialized the list and used it to add pieces of a jigsaw puzzle inside the list.我们初始化了列表并使用它在列表中添加了拼图的碎片。

The user is to input the amount of rows and column they want the board to be.用户将输入他们想要的板的行数和列数。 Each row is a new piece and each column is a new piece.每一行都是一个新的作品,每一列都是一个新的作品。 Each piece is a list of 3 lists to look like a matrix.每个部分都是一个由 3 个列表组成的列表,看起来像一个矩阵。 My idea was to create a list pieces=[] and append the list with each new generated piece.我的想法是创建一个列表pieces=[] 并将列表附加到每个新生成的作品中。

My problem is that i can't get the str() to print the board out.我的问题是我无法让 str() 打印出电路板。 I was thinking we should print the first list of the 3 lists of each piece in the pieces list and then for the middle list of the 3 lists and then the last list of the three lists.我想我们应该打印pieces列表中每个部分的3个列表的第一个列表,然后打印3个列表的中间列表,然后打印三个列表的最后一个列表。 And start at a new line.并从新行开始。 I do know how to implement it though.我确实知道如何实施它。

Or I was thinking i could just print each piece alone and then add pieces to that string to print out the board.或者我想我可以单独打印每一块,然后将碎片添加到该字符串中以打印出电路板。 So i would not be using the pieces=[] list.所以我不会使用pieces=[] 列表。 I don't know which is best or how to implement them.我不知道哪个最好或如何实施它们。

I tried looking at other questions but I could not find an answer, I try to print the string representation of the class:我尝试查看其他问题,但找不到答案,我尝试打印该类的字符串表示形式:

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())

Here is the error I get:这是我得到的错误:

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

You misunderstand the error message: It doesn't say that a list would not be iterable, it says that a method isn't. 您误解了错误消息:它不是说列表是不可迭代的,而是说方法不是。

What happens is that your pieces list doesn't contain pieces but references to the add_piece method because you forgot to call the method when you wanted to append its result in line 56. 发生的情况是您的pieces列表中不包含片段,而是引用了add_piece方法,因为当您希望将其结果附加到第56 add_piece ,您忘记了调用该方法。

You could have found this error even having less experience with exception types by invoking a debugger ( pdb ) just before the line that raises the error. 您甚至可以通过在引发错误的行之前调用调试器( pdb )来发现此错误,甚至对异常类型的经验不足。

谢谢托马斯,你的回答足以让我发现我的问题在我的 x=response.json() 上丢失了 ()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到这个TypeError:&#39;int&#39;对象不是可迭代的python? - Why do i get this TypeError: 'int' object is not iterable python? 为什么我收到此TypeError:“ int”对象不可迭代 - Why do i get this TypeError: 'int' object is not iterable 为什么我一直收到此错误:TypeError:&#39;bool&#39;对象不可迭代? - Why do I keep getting this error: TypeError: 'bool' object is not iterable? 为什么我在尝试迭代列表时得到&#39;TypeError:&#39;numpy.int64&#39;对象不可迭代&#39;? - Why do I get 'TypeError: 'numpy.int64' object is not iterable' when trying to iterate over a list? “TypeError: 'function' object is not iterable”,为什么我会收到这个错误? - "TypeError: 'function' object is not iterable", why am I getting this error? 为什么我收到TypeError“类型&#39;类型&#39;的参数不可迭代”? - Why do i get the TypeError “argument of type 'type' is not iterable”? 为什么我收到错误 TypeError:“名称”类型的参数不可迭代? - Why do I receive error TypeError: argument of type 'Name' is not iterable? 我得到 TypeError: 'int' object is not iterable,但不是关于错误的 - I get the TypeError: 'int' object is not iterable, but not really about the error 类型错误:“方法”object 不可迭代 - TypeError: 'method' object is not iterable 为什么我在 python 中收到错误“TypeError: &#39;NoneType&#39; object is not subscriptable”? - Why do I get the error "TypeError: 'NoneType' object is not subscriptable" in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM