Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会出现这个错误;类型错误:';方法';对象不可编辑;?_Python_Python 3.x - Fatal编程技术网

Python 为什么我会出现这个错误;类型错误:';方法';对象不可编辑;?

Python 为什么我会出现这个错误;类型错误:';方法';对象不可编辑;?,python,python-3.x,Python,Python 3.x,我不明白为什么一份清单是不可接受的。我们初始化了列表,并使用它在列表中添加拼图 用户需要输入他们希望板显示的行数和列数。每一行都是一个新片,每一列都是一个新片。每件作品都是一个由3个列表组成的列表,看起来像一个矩阵。我的想法是创建一个列表pieces=[],并将每个新生成的片段附加到列表中 我的问题是我无法让str()打印出电路板。我在想我们应该先打印3张单子中的第一张单子,然后打印3张单子中的中间单子,最后打印三张单子中的最后一张单子。从一条新的线路开始。不过我知道如何实现它 或者我想我可以单

我不明白为什么一份清单是不可接受的。我们初始化了列表,并使用它在列表中添加拼图

用户需要输入他们希望板显示的行数和列数。每一行都是一个新片,每一列都是一个新片。每件作品都是一个由3个列表组成的列表,看起来像一个矩阵。我的想法是创建一个列表pieces=[],并将每个新生成的片段附加到列表中

我的问题是我无法让str()打印出电路板。我在想我们应该先打印3张单子中的第一张单子,然后打印3张单子中的中间单子,最后打印三张单子中的最后一张单子。从一条新的线路开始。不过我知道如何实现它

或者我想我可以单独打印每一个片段,然后将片段添加到字符串中以打印出电路板。因此,我不会使用片段=[]列表。我不知道哪一个是最好的,也不知道如何实施

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

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
回溯(最近一次呼叫最后一次):
文件“C:/Users/HP/PycharmProjects/untitled/.idea/jigsaw.py”,第102行,在
打印(拼图(3,5)。拼图板()
文件“C:/Users/HP/PycharmProjects/untitled/.idea/jigsaw.py”,第56行,在__
返回str(self)
文件“C:/Users/HP/PycharmProjects/untitled/.idea/jigsaw.py”,第22行,在__
对于行中的列:
TypeError:“方法”对象不可编辑

您误解了错误消息:它并不是说列表不可编辑,而是说方法不可编辑

发生的情况是,您的
片段
列表不包含片段,而是引用了
添加片段
方法,因为您想在第56行追加其结果时忘记调用该方法


在引发错误的行之前调用调试器(
pdb
),即使您对异常类型的处理经验较少,也可能会发现此错误。

请发布一个而不是整个代码。此外,始终包括回溯,以便我们至少可以查看问题发生的位置。尽管从错误中可以清楚地看到,您实际上并没有在列表上进行迭代。运行调试器并找出它崩溃的原因。因此,我删除了rpr()方法以查看它如何通过str()方法,并且它没有按我希望的方式打印。你对我如何解决这个问题有什么想法吗?你需要打电话给add_-piece。add_-piece(),而不是add_-piece。谢谢打印列表!但是,当我运行Jigsaw(3,5)而不调用Puzzboard时,即使在方法中将打印更改为return之后,它也不会通过str方法。它什么也不返回。