我正在尝试将整数传递给函数。我认为它可能不起作用,因为我多次调用它?例如,我在一个名为 Alist 的函数中创建了一个二维矩阵,然后将其返回。使用第二个函数,我通过 Alist 并为我想要从 Alist 中返回的值指定一个位置,然后返回。最后(到目前为止),第三个函数将要求返回值和 Alist。Alist 打印正常,但返回的值(节点)在应该为 4 时打印 0。我猜它正在使用代码顶部声明的 node = 0 变量,但我不知道为什么。
network.txt 的第一行是这样的:0,2,4,1,6,0,
Alist = []
node = 0
file = ("F:/media/KINGSTON/Networking/network.txt")
def create_matrix(file):
with open('network.txt') as f:
Alist = []
for line in f:
part = []
for x in line.split(','):
part.append(int(x))
Alist.append(part)
return Alist
def start_node(Alist):
node = Alist[0][2]
print (node)
return node
#test neighbours to see if they can be used
def check_neighbours(node, Alist):
print (Alist)
print (node)
#check for neighbours. if we start at [0][0] we must look for [0][1]
#and [1][0]. Figure out how to add 1 to each cell to navigate across.
#running of code begins here
Alist = create_matrix(file)
start_node(Alist)
check_neighbours(node, Alist)