georgehere
georgehere

Reputation: 630

Getting the output of a single returned variable out of many in Python

How can I only get either the first or the second return only by running a def function. I am trying to only get the value for the addition for WhatIsA and I am only trying to get the multiplication value for WhatIsB. I know that the code below is not convenient but I just want to know if there is a way to specify the only index of the return values as you run the program like a[0] or b[1]. How would I be able to do that?

def WhatIsA(value1, value2):
    a[0] = Values(value1,value2)
    return a[0]

def WhatIsB(value1, value2):
    b[1] = Values(value1,value2)
    return b[1]


def Values(value1,value2):
    a = value1+value2
    b = value1*value2
    return a, b

print(WhatIsA(10, 15))
print(WhatIsB(2, 12))

Output:

(25, 150)
(14, 24)

Expected Output:

25
24

Upvotes: 0

Views: 133

Answers (2)

user16557139
user16557139

Reputation:

You are returning a tuple from the Values() function. This tuple is created upon assigning a value to Values(). What you want to do is index the tuple upon returning it as follows.

def WhatIsA(value1, value2):
    a = Values(value1,value2)
    return a[0]

def WhatIsB(value1, value2):
    b = Values(value1,value2)
    return b[1]


def Values(value1,value2):
    a = value1+value2
    b = value1*value2
    return a, b

print(WhatIsA(10, 15))
print(WhatIsB(2, 12))

Upvotes: 1

Sither Tsering
Sither Tsering

Reputation: 1308

If your function Values()is a must, then put this a,b = Values(value1,value2) in both the functions WhatIsA and WhatIsB.

def WhatIsA(value1, value2):
    a,b = Values(value1,value2)
    return a

def WhatIsB(value1, value2):
    a,b = Values(value1,value2)
    return b

Update:
you are assigning to a specific index and returning that value. Instead, assign it as whole and use index to return it.

def WhatIsA(value1, value2):
    a = Values(value1,value2)
    return a[0]

def WhatIsB(value1, value2):
    b = Values(value1,value2)
    return b[1]

Upvotes: 2

Related Questions