# Medical Insurance Project, python Fundamentals

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

So I’ve copied this code more or less exactly, but I keep getting this same typeError message:
TypeError: unsupported operand type(s) for -: ‘tuple’ and ‘tuple’

I’m not really familiar with what a “tuple” is and I’m not sure why I’d be hitting this error. Here’s my exact code below. Any ideas why I might be hitting this error message?

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

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

output_message = print(“The estimated insurance cost for " + str(name) + " is " + str(estimated_cost) + " dollars.”)

return output_message, estimated_cost

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

omar_insurance_cost = calculate_insurance_cost(35, 1, 22.2, 0, 1, name = “Omar”)

Austin_insurance_cost = calculate_insurance_cost(29, 1, 26, 0, 0, name = “Austin”)

def diff_insurance_costs(x, y):

diff_cost = x - y

Print(“The difference in insurance cost is " + str(diff_cost) + " dollars.”)

return diff_cost

diff_insurance_costs(omar_insurance_cost, maria_insurance_cost)

If you’re posting code to the forums please see- How do I format code in my posts?

The following link might help you understand and separate lists and tuples- Lists and Tuples in Python – Real Python

As ever be careful just copying code you don’t fully understand, it is often less useful for learning and very high risk for bugs. If you look at your first function you are returning two references, `output_message` and `estimated_cost`. Those references will be wrapped up in a tuple, if you printed that value it’d likely look like `(None, 3474.23)` or something along those lines.

I’d guess you only want to pass the float value (estimated_cost) instead of the tuple you are using at present. To do so either reconsider what you return form your function or only pass the part of the tuple necessary (as with the link above you can index tuples like lists).

Wow, thank you for the quick response! Revising what I returned on my first function did the trick. And thanks for the explanation on code formatting in the forums. First time posting

1 Like