简体   繁体   English

似乎我不能在另一个函数中使用一个函数

[英]It seems i can't use a function inside another function

I'm trying to build a little short path problem I succesfully created the nodes as objects, defined the starting one and the final one with dedicated functions and computed(with a dedicated function) the neighbors inside a radius for any given node using an euclidean distance(dedicated function as well)我正在尝试构建一个小短路径问题我成功地将节点创建为对象,使用专用函数定义了起始节点和最后一个节点,并使用欧几里德计算(使用专用函数)任何给定节点的半径内的邻居距离(还有专用功能)

I'm sure the neighbors function, where nodes is a list nodes and node is the node of which i want to compute the neighboring nodes, works.我确定邻居函数,其中节点是一个列表节点,节点是我想要计算相邻节点的节点,工作。

def neighbors(nodes,node):
    neighbors=[]
    for i in nodes:
        dist=distance(i,node)
        if dist<=15:
            neighbors.append(i)
    return neighbors

when i try and call the function to select a path from the starting node to the final node当我尝试调用该函数来选择从起始节点到最终节点的路径时

def choose_path(final_node,starting_node,nodes):
    neighbors=neighbors(nodes,starting_node)
    more code

after i defined nodes and starting node, it raises an error: "local variable 'neighbors' referenced before assignment" and i can't understand why.在我定义节点和起始节点后,它引发了一个错误:“在赋值之前引用了局部变量‘邻居’”,我不明白为什么。 Am I doing something wrong?难道我做错了什么?

Thank you in advance to those who will help me!在此先感谢那些愿意帮助我的人!

You are trying to call neighbors() and then assign the result to neighbors .您正在尝试调用neighbors() ,然后将结果分配给neighbors

Don't use the same name for your variable as your function.不要为您的变量使用与您的函数相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在另一个函数中使用一个函数 - How can i use a function inside of another function 我可以使用另一个 class 内部的 function 中的变量吗? - Can I use a variable from a function that's inside of another class? Python-为什么我不能在print函数中使用生成器? - Python - Why can't I use generators inside the print function? 在Python的另一个函数中使用函数时,是否可以隐藏函数的某些返回值? - Can I hide some of my function's return values when I use it inside another function in Python? 不能使用另一个函数的变量 - Can't use a variable from another function 为什么我不能在函数内使用vars()作为函数外的变量? - Why I can't use vars() inside my function for a variable that is outside my function? 如何在函数内使用“if”循环? - How can I use the “if” loop inside a function? 不能在 Python 的另一个函数中使用函数的变量 - Can't use the variables of a function in another function for Python 如何在函数Login()中获取&#39;username&#39;的值,以在另一个python程序上使用它? - How can I get the value of 'username' inside the function Login() to use it on another python program? 如何从另一个 class 中调用 function 并在新文件中使用它? - How can I call a function from inside another class and use it in a new file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM