我已经解决了一段时间的问题,只是不确定为什么代码有问题。我试图在 self 下面的机器人类中提供该函数,但从命令行调用 python 文件一直告诉我“名称生成”未定义。
在我使它对命令行友好之前,该程序以前工作过。这条线给了我生成(remainder_of_list)中项目的问题:
##DRIVER CODE!!!####
from robot import point
from robot import robot
import sys
#Input the argv elements
#The elements go into the robot, who travels the maze
sys.argv
coordinates = point(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]))
searcher = robot(coordinates)
one_solution = searcher.find_solutions()
all_solutions = robot.generate(searcher, one_solution)
#Navigate through the solution list
#Because each solution is contained within a list, each list within must be turned into a string
for i in range(len(all_solutions)):
for w in all_solutions[i]:
temp_print_out += w
print(temp_print_out)
temp_print_out = ""
print("Number of paths: " + str(len(all_solutions)))
print(end='\n')```
###CLASSES CODE!!! DIFFERENT .PY FILE####
class point:
def __init__(self, beginning_x, beginning_y, ending_x, ending_y):
self.starting_x = beginning_x
self.starting_y = beginning_y
self.final_x = ending_x
self.final_y = ending_y
class robot:
def __init__(self, point):
self.starting_x = point.starting_x
self.starting_y = point.starting_y
self.final_x = point.final_x
self.final_y = point.final_y
self.all_solutions = []
def find_solutions(self):
#, x, y, goal_x, goal_y
#navigator_x = x
#navigator_y = y
#displacement_x = goal_x - x
#displacement_y = goal_y - y
displacement_x = self.final_x - self.starting_x
displacement_y = self.final_y - self.starting_y
travel_x = abs(displacement_x)
travel_y = abs(displacement_y)
is_north = False
is_south = False
is_west = False
is_east = False
temp_solution = []
solutions = []
#Abs value the displacement to allow the for loops to do their jobs
#The N/S/E/W directions are determined by the bools
if(displacement_x > 0):
is_north = True
if(displacement_x < 0):
is_south = True
if(displacement_y > 0):
is_east = True
if(displacement_y < 0):
is_west = True
#If i > 0, then add N, S otherwise
#If w > 0, add E, otherwise W
for i in(range(travel_x)):
if(is_north == True):
temp_solution.append('N')
elif(is_south == True):
temp_solution.append('S')
for w in(range(travel_y)):
if(is_east == True):
temp_solution.append('E')
elif(is_west == True):
temp_solution.append('W')
return temp_solution
#Recursively find the permutations
#Use a permutation function to find all possible combos (only 2 directions are legal anyway)
#Permutation approach to generating all answers
def generate(self, list):
#Permutation approach to generating legal answers
if len(list) == 0:
return []
if len(list) == 1:
return [list]
#temp_storage = []
for list_index in range(len(list)):
temp_item = list[list_index]
remainder_of_list = list[:list_index] + list[list_index + 1:]
#Recursive approach to generating all legal answers
for item in generate(remainder_of_list):
#DO NOT ALLOW DUPLICATES...inefficient method but quicker...
if ([temp_item] + item) not in all_solutions:
all_solutions.append([temp_item] + item)
return self.all_solutions````
回答1
101
行应该是:
for item in self.generate(remainder_of_list):
必须参照实例或类调用所有方法 (self
)