Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. My idea was to create a list pieces=[] and append the list with each new generated piece.

My problem is that i can't get the str() to print the board out. 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. 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. 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
like image 367
Jessy White Avatar asked Feb 19 '16 16:02

Jessy White


People also ask

How do I fix TypeError int object is not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

How do I fix TypeError float object is not iterable?

Conclusion # The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(int(3.0)): .

Why JavaScript objects are not iterable?

In JavaScript, Object s are not iterable unless they implement the iterable protocol. Therefore, you cannot use for...of to iterate over the properties of an object. const obj = { France: 'Paris', England: 'London' }; for (const p of obj) { // TypeError: obj is not iterable // … } Instead you have to use Object.

Is not iterable type error?

The Python TypeError: NoneType Object Is Not Iterable is an exception that occurs when trying to iterate over a None value. Since in Python, only objects with a value can be iterated over, iterating over a None object raises the TypeError: NoneType Object Is Not Iterable exception.


1 Answers

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.

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.

like image 133
Thomas Lotze Avatar answered Oct 12 '22 23:10

Thomas Lotze