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:
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?
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_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
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
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
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
maria and is an unnecessary assignment anyway (since
individual2 are parameters assigned with the original function call.