0

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

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

3 Comments

Ideal method to use
I have not been able to explain my question very well so i just updated the code please do let me know if that makes more sense
@georgehere okay, understood. I'll update it
1

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))

2 Comments

I know that but I was curious if I can filter out a function with multiple returns. like instead of having a,b = Values(value1,value2) I would just have a[0] = Values(value1,value2). I know that the code above is not convenient just want to filter the return value.
I have updated the code hopefully that is more clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.