Can i use function with multiple return value inside a function , as an argument?

This is the code which i have written for medical insurance project under functions lesson
function to find the cost of insurance with formula given in the exercise
def calculate_insurance_cost(name,age,sex,bmi,num_of_children,smoker):

estimated_cost=250age-128sex+370bmi+425num_of_children+24000*smoker-12500
acknowledgement =“The estimated insurance cost for “+name+” is “+str(estimated_cost)+” dollars.”
print(acknowledgement)
return estimated_cost,acknowledgement

maria_insurance_cost = calculate_insurance_cost(“Maria”,28,0,26.2,3,0)

omar_insurance_cost = calculate_insurance_cost(“Omar”,35,1,22.2,0,1)
my_insurance_cost=calculate_insurance_cost(“Anas”,28,1,24.1,0,0)

Function find the difference between cost of insurance of two persons below
def difference(cost1,cost2):
diff=cost1-cost2
print(“The difference in insurance between 1st and 2nd person”,diff,“dollars”)

difference(omar_insurance_cost,my_insurance_cost)

When i run the code, the below output is shown with error.

The estimated insurance cost for Maria is 5469.0 dollars.
The estimated insurance cost for Omar is 28336.0 dollars.
The estimated insurance cost for Anas is 3289.0 dollars.
Traceback (most recent call last):
File “script.py”, line 19, in
difference(omar_insurance_cost,my_insurance_cost)
File “script.py”, line 16, in difference
diff=cost1-cost2
TypeError: unsupported operand type(s) for -: ‘tuple’ and ‘tuple’

I was trying to assign one of the values of returned values of a the first function as an argument to second function.

Does anybody know how to do that?

If possible could you edit your code to correctly format it on the forums, the following link can help- How do I format code in my posts?

A tuple is fairly similar to a list with the biggest different being that the tuple itself cannot be altered (mutated) like a list can. You can index/unpack values as usual though.

If your function returns a tuple of two items you could assign those two items to different names, e.g. val1, val2 = myfunc(). Then you can just use whichever name contains the values you want. Or, you can index it just like a list, if you want the first element of the tuple for example use mytuple[0].

2 Likes

Thank you. @tgrtim . Will definitely use your suggestions for code formats.