繁体   English   中英

为什么这个 python 代码不起作用(可能是语法错误?)

[英]Why is this python code not working (possible syntax error ?)

我是 python 的新手,不明白为什么 goSouthWest 方法不适用于我的测试仪。 这两个文件位于同一个文件夹中。 出现的问题是“未定义 goSouthWest”。 在 getPath function 中调用 goSouthWest function 时基本上是名称错误。 有人可以告诉我我的代码有什么问题吗? 测试仪的内容如下。

测试人员:

from Map import Map  

map1 = Map(10, 10) 

print(map1.getPath(5, 5, 4, 1))

Class:

import math

class Map:

    def __init__(self, row, column):
        self.row = row
        self.column = column

    def getPath(self, startRow, startCol, destRow, destCol):
        if startRow < 0 or startRow > self.row or startCol < 0 or startCol > self.column or destRow < 0 or destRow > self.row or destCol < 0 or destCol > self.column:
            raise ValueError("IllegalArgumentException")
        elif (startRow >= destRow and startCol >= destCol):
            path = goSouthWest(startRow, startCol, destRow, destCol)
        return path

    def goSouthWest(startRow, startCol, destRow, destCol): # goSouthWest method
        colDiff = startCol - destCol
        rowDiff = startRow - destRow
        if (colDiff > rowDiff) and (startRow != destRow):
            startRow -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (rowDiff > colDiff) and (startCol != destCol):
            startCol -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (rowDiff > colDiff) and (startCol == destCol):
            startRow -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (colDiff > rowDiff) and (startRow == destRow):
            startCol -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (rowDiff == 0) and (colDiff == 0) and (destRow == 0) and (destCol == 0):
            startCol += 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        else:
            startRow -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "

        if startCol != destCol and startRow != destRow:
            path = path + goSouthWest(startRow, startCol, destRow, destCol)

        return path

您在goSouthWest中没有self参数。 此外,您应该将成员函数称为self.function() ,例如path = self.goSouthWest(startRow, startCol, destRow, destCol)

你的代码应该是这样的:

import math

class Map:

    def __init__(self, row, column):
        self.row = row
        self.column = column

    def getPath(self, startRow, startCol, destRow, destCol):
        if startRow < 0 or startRow > self.row or startCol < 0 or startCol > self.column or destRow < 0 or destRow > self.row or destCol < 0 or destCol > self.column:
            raise ValueError("IllegalArgumentException")
        elif (startRow >= destRow and startCol >= destCol):
            path = goSouthWest(startRow, startCol, destRow, destCol)
        return path
                    # every method of a class must have a self argument
    def goSouthWest(self, startRow, startCol, destRow, destCol):
        colDiff = startCol - destCol
        rowDiff = startRow - destRow
        if (colDiff > rowDiff) and (startRow != destRow):
            startRow -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (rowDiff > colDiff) and (startCol != destCol):
            startCol -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (rowDiff > colDiff) and (startCol == destCol):
            startRow -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (colDiff > rowDiff) and (startRow == destRow):
            startCol -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        elif (rowDiff == 0) and (colDiff == 0) and (destRow == 0) and (destCol == 0):
            startCol += 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "
        else:
            startRow -= 1
            path = path + "(" + str(startRow) + "," + str(startCol) + ") "

        if startCol != destCol and startRow != destRow:
                          # methods of the class must be called with self
            path = path + self.goSouthWest(startRow, startCol, destRow, destCol)

        return path

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM