Medical Insurance Project, python Fundamentals

Link to the exercise

I’m a baby newby beginner and I’m having trouble with the last part of this. I don’t think the lesson covered how to do the second “extra practice” part. I’m not entirely sure how to go about creating a second function that uses returned values from a previous function.

here is my code:

1 Like

I’m not 100% sure which lessons were covered up to this point but if I’m not mistaken you’d have two variables saved, new_insurance_cost which is the most recent usage and insurance_cost which you calculated in step 2. You’re looking for the difference between the two which is just a - b.

Passing the output of functions to one another is fairly straightforward if that became necessary but I don’t think it’s required for the this step (if I’m mistaken then I do apologise). For example both print and str are functions. The line print(str(3)) would evaluate str(3) and then it’s return, the string '3', would be passed to print. If it gets too complex then use intermediate variables or consider re-writing your code (it should remain readable to a human being).

I dont think it wants the difference in cost between old and new, but a difference in cost between two individuals. I’m just not sure how to write a function to calculate the differnce between maria and Omar’s costs

Sorry. I seem to have wound up looking at a very similar but somewhat different project. I’m not entirely sure what kind of function they want. Technically if you’ve saved maria_insurance_cost and omar_insurance_cost and it had to be a function then you could just create a function that calculates the difference of any two inputs (e.g. a and b) and returns the expected string once it is formatted.

Whilst you could call the first function from within your new function like so-

def insurance_difference():
    cost1 = calculate_insurance_cost(...
    cost2 = calculate_insurance_cost(...
    return cost1 - cost2

It’s always going to be a mess like this because you are either hard-coding part of the function or passing several arguments at once. With a little more work such as wrapping all of Maria’s data and all of Omar’s data into their own sequences or using something like *args (which may not have been introduced and you can ignore for now) I can’t see a nice way to do it.

I was stuck on this one as well, and revisited it today and came up with a working code that addresses what you and I were both thinking, in that there had to be a way to pull the insurance cost from the prior function and use that in the new function.

Below is my work, I hope it helps and am available if you have any questions.

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

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

  print(estimated_cost)

  return estimated_cost

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

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

charles_insurance_cost = calculate_insurance_cost(38, 1, 50, 3, 1, "Charles")

def difference_insurance_cost(x, y):

  diff_cost = x - y

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

  return diff_cost

difference_insurance_cost(omar_insurance_cost, maria_insurance_cost)

I have two questions about the assignment of variables to the function call here. In this section of code:

maria_insurance_cost = calculate_insurance_cost(28, 0, 26.2, 3, 0, 'Maria') 
 
omar_insurance_cost = calculate_insurance_cost(35, 1, 22.2, 0, 1, 'Omar')

ben_insurance_cost = calculate_insurance_cost(43, 1, 23.8, 3, 0, 'Ben')

It looks to me like “maria_insurance_cost” is a variable that is assigned to the function. So if I “call” the function using the variable, it should output the same thing. I tried it like this “print(maria_insurance_cost)” and that outputs the “estimated_cost” only, not the whole sentence. I also tried to just type “maria_insurance_cost” alone as if it were the function. And that outputs nothing.

Also, why do we need the line: “return estimated_cost”? Using print(estimated_cost) gives us the solution to the first part of the Extra question. It prints the cost on a separate line after the full sentence. But if I delete the line altogether, the function still works the same. I’d love to hear someone explain what is going on here.

So, I answered part of my own question and I have a solution to another. The line “return estimated_cost” doesn’t seem to matter in the original function. BUT, when using that function inside another function, it is necessary in order to use that calculated value. I came up with this code which was so simple I didn’t think it would work:

maria = calculate_insurance_cost(28, 0, 26.2, 3, 0, 'Maria') 
 
omar = calculate_insurance_cost(35, 1, 22.2, 0, 1, 'Omar')

me = calculate_insurance_cost(43, 1, 23.8, 3, 0, 'Ben')

def cost_difference(person1, person2):
  difference = person1 - person2
  print("The difference in insurance cost is " + str(difference) + " dollars.")

cost_difference(omar, me)

This outputs “The difference in insurance cost is 20133.0 dollars.” It took the returned value “estimated_cost” without the text strings and applied that to my new function. so cool.

You’re quite right, typically you’d return from most functions as it gives you more options; as you mentioned in your second post, it is necessary when you want to continue working with the output. Whilst using print to output data can be useful under certain circumstances you may find yourself using it little outside of an interactive python session (and/or debugging) as there are other ways of logging and writing to files.

You mentioned using the variable name alone which provided no output whereas a usage might be more along the lines of print(calculate_insurance_cost(28, 0, 26.2, 3, 0, 'Maria')) or myvar = calculate_insurance_cost(28, 0, 26.2, 3, 0, 'Maria') followed by print(myvar) where the function calculate_insurance_cost has a return statement. It’s not a lot more typing and makes your function much more versatile.

So for this specific problem perhaps using print only inside the function best matches the requirements but generally using return gives you many more options with your function (which is kind of the main point of functions).