【问题标题】:Why is this python code not working (possible syntax error ?)为什么这个 python 代码不起作用(可能的语法错误?)
【发布时间】:2021-04-11 15:41:20
【问题描述】:

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

测试人员:

from Map import Map  

map1 = Map(10, 10) 

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

类:

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

【问题讨论】:

  • 欢迎来到 SO!请使用代码格式化工具,使每个文件中的所有代码都显示为一个块(突出显示代码并单击 {} 按钮)。

标签: python syntax nameerror undefined-function


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你的代码应该是这样的:

    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
    

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 2019-12-11
      • 2016-07-02
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      相关资源
      最近更新 更多