Reputation: 11
I am new to python and cannot understand why the method, goSouthWest, is not working with my tester. Both files are located in the same folder. The issue that arises is "goSouthWest is not defined". Basically a name error when calling the goSouthWest function in getPath function. Could someone please tell me what's wrong with my code? The tester's contents are as follows.
Tester:
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
Upvotes: 0
Views: 153
Reputation: 5992
Your code should be like this:
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
Upvotes: 0
Reputation: 987
You don't have the argument self
in goSouthWest
. Also, you should call member functions as self.function()
, like path = self.goSouthWest(startRow, startCol, destRow, destCol)
Upvotes: 2