[英]How to reuse a function within the same python script
I am writing a program and I would like to call twice the same function but with different parameter values.我正在编写一个程序,我想调用相同的函数两次,但参数值不同。
def finding_numbers(x, y, z):
"""some code here"""
return z
def one():
"""some code here"""
return
def try_again(e,t,q):
p = finding_numbers(x=e,y=t,z=q)
return p
def main():
finding_numbers(x,y,z)
one()
try_again(e,t,q)
main()
I have tried to call the function as the code above but I don't get the expected return, in fact, it returns nothing.我试过像上面的代码那样调用该函数,但我没有得到预期的回报,事实上,它什么都不返回。 I have tried to create a function with different name
def try_again(finding_numbers(x=e,y=t,z=q))
but it does not work.我试图创建一个具有不同名称的函数
def try_again(finding_numbers(x=e,y=t,z=q))
但它不起作用。 I have also tried to call it from the main again as finding_numbers(x=e,y=t,z=q)
.我还尝试再次从主函数中将其调用为
finding_numbers(x=e,y=t,z=q)
。 I have been ready about how to re-use function within the same python script and I cannot find anything suitable.我已经准备好如何在同一个 python 脚本中重用函数,但我找不到合适的东西。 How to process this?
如何处理?
you can call the function finding_numbers inside try_again, instead of passing it in as a parameter:您可以在 try_again 中调用函数 finding_numbers,而不是将其作为参数传入:
def try_again(e, t, q):
return finding_numbers(e, t, q)
For your example, your 'try_again' function is not written according to your description.对于您的示例,您的“try_again”函数未根据您的描述编写。
def try_again(e, t, q):
q = finding_numbers(x=e, y=t, z=q)
return q
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.