【发布时间】: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