does anyone have the code solution for Task 13 (Extra) of the Python Functions: Medical Insurance Project? would be super grateful as I can’t crack it!
Welcome to the forums!
Do you have a link? Or can you copy/paste what it asks for?
There’s a couple of possible tasks here. What have you tried so far?
For some mild hints you can return
multiple objects from a single function by wrapping them in a list or similar. Separating them with a comma is common too (it technically creates a tuple), e.g.
def get_one_two():
return 1, 2
val_one, val_two = get_one_two()
You already have a function you can call for calculating costs. If you need two different costs…
having completed the first 12 steps I am left with the following code: gist.github.com/82590dc77113a59a2860680779f83fee
Task 13’s instructions are:
- Modify the
calculate_insurance_cost()
function so that it returns two values – the output message and the estimated cost. - Create a second function to calculate the difference between the insurance costs (given as inputs) of any two individuals and print a statement saying:
"The difference in insurance cost is xxx dollars."
what would you do? (sorry for the late response)
Since the code already prints a message and returns a value then I’d interpret that instruction in the way I mentioned above where you’d return two objects (the cost and the output message) but I may be wrong.
As for calculating the insurance cost difference there’s a few ways to do so. You seem to be most of the way there but I believe the intention was to avoid hard-coding anything. The calculation seems reasonable cost_difference = individual1 - individual2
but the re-assignment inside the function seems off (individual1 = maria_insurance_cost
). Consider exactly what steps you need to take here and remove anything unnecessary.
Since it seems set up to do so why not pass your costs as arguments to your new function in the style of: myfunction(argument1, argument2)
.
Once that’s cleaned up you can modify your print statement like you did in your first function to concatenate strings in order to get the specified output.
For the first instruction of Task 13 (Modify the calculate_insurance_cost()
function so that it returns two values – the output message and the estimated cost); it seems to me that this function is already returning two values - the estimated_cost
variable and the print function/output message (“The estimated insurance cost for " + name + " is " + str(estimated_cost) + " dollars.”)
Given the instruction, I don’t see what difference modifying the calculate_insurance_cost()
would make?
thank you for your help and patience!
My interpretation for this instruction was that since it already prints a message and returns a value it had to be taken as modifying your return statement to supply two values along the following lines-
def two_ones():
return 1, 1
Whether or not that’s true I don’t know, I’d say the important bit is you understand how it could be done.
I have tried the following code to calculate the cost difference between Maria and Omar’s insurance: gist.github.com/eca196ab3c281dac506a1b9ddc6b022d - lines 7-9. But I am not getting any output in the terminal for cost difference?
The function calculate_cost_difference
is never called so it wouldn’t ever be executed and you will not receive any output from that function. Make sure you call it with the relevant arguments.
I think it’s also worth altering the function so that it doesn’t have parameter names that re-use outer scope names like omar_insurance_cost
. It should be something like insurance_cost1
and insurance_cost2
or something similar as the function is designed to calculate the difference between any two insurance costs. Just pass omar_insurance_cost
or similar as the argument to this function.
By ‘called’ do you mean typing return cost_difference
? because I tried that but did not get any output in the terminal
Yes I agree, I originally used indivudal1
and individual2
so that any insurance costs could be applied to them. But when you said ‘Consider exactly what steps you need to take here and remove anything unnecessary’ I thought you meant streamline by reducing the amount of code
Sorry I’m still unclear
No, return
is used when defining a function. Calling a function typically takes on the form function(args)
that is the function name followed by parentheses containing the arguments that are to be passed to the function. In the following example print
is called with an argument of integer 3-
print(3)
You need to call your function with the relevant arguments (based on your function definition this would be the insurance costs of two individuals).
Ahh no I was talking about a reassignment inside the function, namely the two lines of the form individual1 = maria_insurance_cost
which means your function only ever calculates the difference of omar
and maria
and is an unnecessary assignment anyway (since individual1
and individual2
are parameters assigned with the original function call.
Hey there! Jumping in here seeking some guidance (complete newbie here so bear with me!) - this is what my code is looking like; https://gist.github.com/d3f9762eac0c13e358b1979903e1122a
I mean, I think i’m following along with the Cheatsheet etc, but i’m clearly going wrong somewhere, any help would be greatly appreciated
DM
Hey Guys,
I have been struggling with the same exercise. This was the only way I could get it to work, however, I don’t know if it is efficient code.
Any help would be appreciated!
# Create calculate_insurance_cost() function below:
def calculate_insurance_cost(name, age, sex, bmi, num_of_children, smoker):
return 250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500
# Estimate Maria's insurance cost
maria_insurance_cost = calculate_insurance_cost(name = "Maria", age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0)
# Estimate Omar's insurance cost
omar_insurance_cost = calculate_insurance_cost(name = "Omar", age = 35, sex = 1, bmi = 22.2, num_of_children = 0, smoker = 1)
# second function
def difference_in_costs(cost_1, cost_2):
print("The difference in insurance cost is " + str(cost_1 - cost_2) + " dollars.")
difference_in_costs(maria_insurance_cost, omar_insurance_cost)
It is a little different from the target. I added two more parameters to the new function
# 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
output_message = "The estimated insurance cost for " + name + " is " + str(estimated_cost) + " dollars."
return (output_message, estimated_cost)
# Defining the new function for calculation the difference in insurance cost between two individuals
def difference_insurance_cost(output_message1,individual1, output_message2, individual2):
print(output_message1)
print(output_message2)
print("The difference in insurance cost between them is " + str(abs(individual1 - individual2)) + " dollars.")
# Estimate Maria's insurance cost
maria_output_message, maria_insurance_cost = calculate_insurance_cost(28, 0, 26.2, 3, 0, name="Maria")
# Estimate Omar's insurance cost
omar_output_message, omar_insurance_cost = calculate_insurance_cost(35, 1, 22.2, 0, 1, name="Omar")
# Calling the function for calculate a difference in cost of insurance between two individuals
difference_insurance_cost(maria_output_message, maria_insurance_cost, omar_output_message, omar_insurance_cost)
Output:
The estimated insurance cost for Maria is 5469.0 dollars.
The estimated insurance cost for Omar is 28336.0 dollars.
The difference in insurance cost between them is 22867.0 dollars.
Hey there @inoue.julian Looks good to me! might sound like a silly question but i’m a little unclear as to how the function ‘knows’ what cost_1 and cost_2 is as it hasnt been previously defined as in ‘cost_1 = etc etc’ - could you perhaps help me clear this up? thanks!
Hey @micro1295706167
I passed Maria’s insurance cost in as cost_1 and Omar’s insurance cost as cost_2
def difference_in_costs(cost_1, cost_2):
print("The difference in insurance cost is " + str(cost_1 - cost_2) + " dollars.")
difference_in_costs(maria_insurance_cost, omar_insurance_cost)
maria_insurance_cos = cost_1 and omar_insurance_cost = cost_2. It then takes these values and passes them through the print().
Create calculate_insurance_cost() function below:
def calculate_insurance_cost(name, age, sex, bmi, num_of_children, smoker):
estimated_cost = 250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500
print("The estimated insurance cost for " + name + " is " + str (estimated_cost) + " dollars.")
return estimated_cost,
maria_insurance_cost = calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2,num_of_children = 3, smoker = 0, name = “Maria”)
omar_insurance_cost = calculate_insurance_cost(age = 35, sex = 1, bmi = 22.2, num_of_children = 0, smoker = 1, name = “Omar”)
robert_insurance_cost = calculate_insurance_cost(age = 23, sex = 1, bmi = 20.2, num_of_children = 0, smoker = 1, name = “Robert”)
def difference_in_costs(individual_1, individual_2):
cost = difference_in_cost(individual_1 = omar_insurance_cost, individual_2 = maria_insurance_cost)
print(“The difference in cost is” + cost + " dollars.")
return cost
This is my code and I can’t get an output for the difference between the insurance cost. If someone can please point out what I am doing wrong, I would greatly appreciate it.
Hello @aeosytb , welcome to the forums! Where do you actually calculate the difference in cost?
Thanks for this! This problem was driving me crazy. I think having the return message in my original function may have been confusing me.