Python用户定义函数

2024-10-06 11:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经阅读了用户定义python函数的this教程。你知道吗

本教程说:

def sum( arg1, arg2 ):
   total = arg1 + arg2
   return total;

# Now you can call sum Function
total = sum( 10, 20 );
print "Outside the function : ", total

在我的例子中,我有一个python函数:

def myf(arg1):
   .................
   some python progress
   ...................
   return out1,out2,out3,out4,out5,out6

最后,我的主函数有6个输出

但是如果我试着像本教程那样调用函数:

myf = out1(myvar)

然后显示来自out1,out2,out4=3,out4,out5,out6的所有输出,而不是我想要的具体位置。你知道吗

例如,我的函数的正确输出是:

out1=10,out2=30,out3=300,out4=12,out5=47,out6=77

myf = out1(myvar)告诉我:

(10,30,300,12,47,77) and not `10` where i want...

你知道如何从输出中得到我所需要的吗?你知道吗


Tags: 函数returndef教程totalsumarg1arg2
1条回答
网友
1楼 · 发布于 2024-10-06 11:26:59

您的函数正在返回一个值元组,这个元组被分配给myf。你知道吗

如果只需要元组中的第n个值,可以通过myf[n]引用它。你知道吗

在您的例子中,您正在寻找myf[0]。你知道吗

>>> print(myf[0])
10

相关问题 更多 >