在函数之间传递返回的变量

时间:2017-11-08 19:54:24

标签: python function

我花了好几个小时试图弄清楚如何将值传递给只打印它接收的函数。

fun1(50)    

def fun1(salary):
       salary_double = salary * salary
       salary_tax = salary 1.04
       return salary, salary_double, salary_tax

def fun2():
       print (salary, salary_double, salary_tax from fun1)

如何将fun1中返回的变量的值传递给fun2?

2 个答案:

答案 0 :(得分:3)

正如fun1需要参数才能工作,fun2也是如此。您可以使用*args制作此可变参数,或指定每次都应接收3个值

def fun2(*args):
    print(*args)

def fun2(a, b, c):
    print(a, b, c)

但是此时您只是复制print来电。请从fun1执行此操作。

答案 1 :(得分:1)

如果我在这里理解了这个问题,请告诉我:

def fun1(salary):
       salary_double = salary * salary
       salary_tax = salary * 1.04
       return salary, salary_double, salary_tax

def fun2():
  salary, salary_double, salary_tax = fun1(50)
  print (salary, salary_double, salary_tax)

但你可以直接做:

 def fun3():
  for x in fun1(50):
    print(x)

def fun3():
  print(fun1(50))

或@ Adam的方式:

def fun4():
  print(*fun1(50))