英文:
Python for loop calls a function that calls another function
问题
我正在使用for循环来遍历一个交换机列表。
对于switch_list中的每个设备,我调用function1。
然后,function1调用function2。
但是,这就是处理结束的时候。
我需要回到for循环,以便处理switch2、switch3等...
这是输出:
我们在主函数中
我们在function1中,设备名称是switch1
我们在function2中,设备名称是switch1
这是我的代码:
switch_list = ['switch1', 'switch2']
def main():
print('我们在主函数中')
for device in switch_list:
main_action = function1(device)
return(device)
def function1(device):
print(f'我们在function1中,设备名称是{device}')
function1_action = function2(device)
def function2(device):
print(f'我们在function2中,设备名称是{device}')
if __name__ == '__main__':
main()
任何帮助将不胜感激。
英文:
I am using a for loop to iterate over a list of switches.
For each device in switch_list, I call function1.
Function1 then calls function2.
However, that's when the processing ends.
I need to get back to the for loop so that I can process switch2, switch3, etc...
Here is the output:
We are in main
We are in function1 and the device name is switch1
We are in function2 and the device name is switch1
Here is my code:
switch_list = ['switch1', 'switch2']
def main():
print('We are in main')
for device in switch_list:
main_action = function1(device)
return(device)
def function1(device):
print(f'We are in function1 and the device name is {device}')
function1_action = function2(device)
def function2(device):
print(f'We are in function2 and the device name is {device}')
if __name__ == '__main__':
main()
Any assistance would be greatly appreciated.
答案1
得分: 1
根据Alexander的建议,return关键字退出函数,将提供的值返回到调用方法的地方。
例如:
def give_10():
return 10
print("我无法达到,因为我在return语句之后。")
print(give_10()) # give_10() 返回10,使该语句等同于print(10)。然后处理该值并打印到标准输出。
英文:
As suggested by Alexander, the return keyword exits the function, returning a provided value to the place where the method was called.
ex.
def give_10():
return 10
print("I am unreachable because I am after a return statement")
print(give_10()) # give_10() returns 10 which makes the statement
# as print(10). Which in turn processes the value and prints to stdout.
答案2
得分: 1
因为在你的主函数中return()
语句位于for循环内部。如果将它移出for循环,你的问题就会得到解决。
return()
代表函数的结束。所以当你的代码遇到return语句时,它会退出main()函数,因此你只会得到第一个设备的输出。
你可以在for循环中创建一个设备列表,然后在完成后将其传递出去。
类似这样:
def main():
main_output_list = []
print("We are in main")
for device in switches:
main_action = function1(device)
main_output_list.append(main的输出)
return(main_output_list)
英文:
It's because the return()
statement in your main function is inside the for loop. Your problem would be solved if you take it out of the for loop.
return()
symbolizes the end of a function. So when your code sees the return statement, it exits main() and you get output for only the first device.
You can create a list of the devices since you are running a for loop and pass it on after it's completion.
Something like this:
def main():
main_output_list = []
print("We are in main")
for device in switches:
main_action = function1(device)
main_output_list.append(output of main)
return(main_output_list)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论