Having an issue understanding how to apply the function without the tuple

https://www.codecademy.com/paths/data-analyst/tracks/dacp-python-fundamentals/modules/dscp-python-functions/projects/ds-python-functions-project

[codebyte]

# Create calculate_insurance_cost() function below: 

def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker, name):

  estimated_cost=250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500

  return estimated_cost, print("The estimated insurance cost for " +name+" is "+ str(estimated_cost) + " dollars.")

def insurance_cost_difference(name1, name2):

  difference = name1 - name2

  return print("The difference in insurance cost is " + str(difference) + " dollars.")

# Estimate Maria's insurance cost

maria_insurance_cost = calculate_insurance_cost(28, 0, 26.2, 3, 0, "Maria")

# Estimate Omar's insurance cost 

omar_insurance_cost = calculate_insurance_cost(35, 1, 22.2, 0, 1, "Omar")

insurance_cost_difference(maria_insurance_cost, omar_insurance_cost)

[/codebyte]

Preformatted textThe issue I am currently dealing with is my understanding of how to apply the inputs to the function. What my assumption was while programming was that the prior function could be applied, whether stored as a variable like this, ‘maria_insurance_cost’, or, ‘omar_insurance_cost’, but I was mistaken. Any and all help to assist me in how to further dissect the arguments that can be applied will be appreciated.

Look at your calculate_insurance_cost function; specifically the return statement. When you write

return something, something_else

Python automatically converts it to a tuple (which is like a list but it can’t be changed).

You are calculating the insurance for Maria and Omar using the calculate_insurance_cost function. You are then passing the return value from that function into the insurance_cost_difference function. But remember that this value is actually a tuple due to how you’ve set up the return statement.

1 Like

You probably shouldn’t be returning a print statement. You’re better off either printing it within your function or returning as a string.
The tuple is occurring because you’re returning two things into one - the estimated_cost variable and the print() call. So, maria_insurance_cost will look something like [9999, none] - where 9999 is the whatever the calculated cost is.
The error is then occurring because when it gets to where it’s working out the difference it’s trying to do it with two tuples and doesn’t know how to process them. e.g. [9999, none] - [9999, none] ?

As I say, moving the print call would solve your issue in this case as then only estimated_cost would be returned, and maria and omar insurance cost variables would just contain the value you were expecting.

Alternatively,
you could use two variables when calling calculate_insurance_cost
e.g.
maria_insurance_cost , something_else = calculate_insurance_cost ( …)

but as something_else would always equal none it wouldn’t be of much use.

1 Like