-1

This is a much-simplified version of my program but has the same idea:

def calc():
   x=1
   y=2
   z=6
def output():
   final=x+y+z

how can I use the return values in the first function to be used in the second?

3 Answers3

2

The method calc must return the values to allow output to retrieve them when calling it.

I deliberately changed the name of the variables in the output method, so you see, they are not related and can be named as you want.

def calc():
    x = 1
    y = 2
    z = 6
    return x, y, z

def output():
    a, b, c = calc()
    final = a + b + c
    print(final)  # 9

if __name__ == '__main__':
    output()
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
azro
  • 53,056
  • 7
  • 34
  • 70
0
def calc():
    x=1
    y=2
    z=6
    return x + y + z # you need to use 'return' in order for function to return some values

def output():
    final = calc()

or

def calc():
    x=1
    y=2
    z=6
    return x, y, z # you need to use 'return' in order for function to return some values

def output():
    x, y, z = calc()
    final = x + y + z
David Delač
  • 359
  • 2
  • 12
0

Yet another option:

def calc():
    x=1
    y=2
    z=6
    return x, y, z

def output(values):
    x, y, z = values
    final = x + y + z
    return final

print(output(calc()))