Python Functions: Medical Insurance Project - Task 13 (Extra)

I think task no.13 has 2 different question :

  1. Modify the calculate_insurance_cost() function so that it returns two values – the output message and the estimated cost.
    2.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.”

because when I combined the question, it would return tuple tuple, so I decide to make 2 answer.

Any feedback would be greatly appreciated

Hey all, I was stuck on the last one as well but thanks to all your answers and some Googling I got it working. I included an if statement that turned a negative outcome into a positive, ie always writing “The diff is xxx…” not “The diff is **-**xxx…”

Here’s how I did it:

def insurance_diff(a,b):
  diff = (a - b)
  if diff < 0:
    print("The difference in insurance costs is: " + str(diff*(-1)) + " dollars.")
  else:
     print("The difference in insurance costs is: " + str(diff) + " dollars.")

Hi, people i just finished this task and here is the code i did and worked, if it helps

# Create calculate_insurance_cost() function below: 

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

  estimaded_cost=(250*age)+(128*sex)+(370*bmi)+(425*num_of_children)+(24000*smoker)-12500

  ##print("The estimaded insurance cost for "+name+" is "+str(estimaded_cost)+" dollars")

  return estimaded_cost, print("The estimaded insurance cost for "+name+" is "+str(estimaded_cost)+" dollars")

def dif_insurance_cost(cost1, cost2):

  dif=cost1-cost2

  ##print(dif)

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

# Initial variables for Maria 

 

# Estimate Maria's insurance cost

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

##print("The estimated insurance cost for Maria is " + str(insurance_cost) + " dollars.")

# Initial variables for Omar

# Estimate Omar's insurance cost 

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

##print("The estimated insurance cost for Omar is " + str(insurance_cost) + " dollars.")

my_insurance_cost=calculate_insurance_cost("Adolfo", 33, 1, 55.6, 1, 0)

omar_maria_diference=dif_insurance_cost(omar_insurance_cost[0], maria_insurance_cost[0])

any feedback is welcome

1 Like

Hello,

Ditto on the struggles of task 13, it is causing my brain to melt. I’m a total newbie to coding. If anyone can help me please?

To recap the task’s instructions:

  1. Modify the calculate_insurance_cost() function so that it returns two values – the output message and the estimated cost.
  2. 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."

For the first part, I’m not sure if I have modified ‘calculate_insurance_cost()’ correctly? It seems a bit redundant to have output_msg in the return? Unless I’ve interpreted the question incorrectly :woman_shrugging:

For the second part, I have defined the difference function at the end of the code but I can’t seem to get any output in the terminal (is that the correct terminology?) I get the following error message:

Traceback (most recent call last):
File “script.py”, line 35, in
diff_in_cost(maria_insurance_cost, omar_insurance_cost)
File “script.py”, line 31, in diff_in_cost
diff = abs(a-b)
TypeError: unsupported operand type(s) for -: ‘tuple’ and ‘tuple’

@tgrtim you provided a link explaining what a Tuple is but it may as well have been in Sumerian :confounded:

This is my code so far…

# 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
  output_msg = "The estimated insurance cost for " + name + " is " + str(estimated_cost) + " dollars."
  print(output_msg)
  return output_msg, estimated_cost


# Initial variables for Maria 
age = 28
sex = 0  
bmi = 26.2
num_of_children = 3
smoker = 0  

# Estimate Maria's insurance cost
maria_insurance_cost = calculate_insurance_cost("Maria", age, sex, bmi, num_of_children, smoker)

# Initial variables for Omar
age = 35
sex = 1 
bmi = 22.2
num_of_children = 0
smoker = 1  

# Estimate Omar's insurance cost 
omar_insurance_cost = calculate_insurance_cost("Omar", age, sex, bmi, num_of_children, smoker)


def diff_in_cost(a, b):
  diff = abs(a-b)
  print("The difference in insurance cost is " + str(diff) + " dollars.")
  return diff

diff_in_cost(maria_insurance_cost, omar_insurance_cost)

2 Likes

I think for the first part of this task it would make more sense if the function did not print the output message by default. So you return the message and the estimated cost, you then have the option of printing the returned message outside the function if you want to. Since it’s an extended task I don’t think you need to worry about whether your example is particularly useful, just be aware that you can do this.

Tuples are actually simpler than lists but they often get overlooked for the more familiar list type so most folks are just more comfortable with lists. At some point you’ll want to understand tuples but if you’re entirely unfamiliar with them at the moment then consider using a list for now. In this task where you’re simply indexing values they operate in the same way anyway.

I think your code for the difference is almost there but remember the value you returned from the first modified function is a container with more than one value.

So your calculation looks a bit like the following (using lists instead of tuples) which has no obvious solution and will give you a type error instead

["words", 10] - ["text", 30]

Consider how you would get the correct value from a list and apply the same logic here before doing the calculation.

1 Like

@tgrtim thank you so much for getting back to me.

Ok, so I think I’ve fixed the first part by getting rid of the print statement completely and converting the return statement to look more like a mathematical formula. I then add the print statement after each each call (lol, hopefully I’m using the right terminology) and that seems to work :raised_hands:

As for the second part, based on your explanation, it seems as though my current code is trying to subtract and take an absolute value of two strings as well as numbers… which is obviously just ridiculous?? (I think I’m having the beginnings of a Homer Simpson moment) So I need to figure out how to bypass the strings contained inside my return from the first function? Or, alter the return statement from my first function so that it just gives me the number to use in my calculation?

1 Like

Terminology in programming is a bit loose sometimes, different languages sometimes have entirely different rules about what even words like ‘function’ mean but what you said makes sense to me so hopefully it makes sense to everyone else too :laughing:.

Yes, at present the code is trying to subtract two containers and the interpreter just doesn’t know what to do so it plays it safe and just throws an error instead. It sounds like you now you know what you need so that’s half the battle.

Both your solutions seem reasonable. Just remember how you normally worked with lists when you want single elements.

:sparkles: Hallelujah @tgrtim Ok thank you so much, it finally clicked lol… now I understand why tiomamelesch added the extra parameters above. Because this is about returning multiple values and the need to create a distinction between each part when you input them into the new function… Since the original function return had 2 values, therefore the new function needs double the amount of parameters. Crikey that only took me 24 hours and enough brain power to power the national grid :sweat_smile: but hey, it’s the journey apparently… thank you so much for your help!!

1 Like

Hello there! I’m kinda stuck too.
I did this but I don’t know if it’s right.

Not sure if i am interpreting the Task 13 correctly. I have pasted my codes below.

Create calculate_insurance_cost() function below:

def calculate_insurance_cost(name, age,sex,bmi,num_of_children,smoker):
estimated_cost = 250age - 128sex + 370bmi + 425num_of_children + 24000*smoker - 12500
print(f"The estimated insurance cost for {name} is {estimated_cost} dollars.")
print(estimated_cost)
return estimated_cost

Create diff_insurance() function below:

def diff_insurance(cost1,cost2):
diff = cost1 - cost2
print(f"The different in insurance cost is {diff} dollars.")
return diff

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)

Calculating the difference in insurance

diff_insurance(maria_insurance_cost,omar_insurance_cost)

hi there, my solution is similar to yours but im not quite understand the part where u put omar_insurance_cost[0] and maria_insurance_cost[0], i know that is a list but what does it tell?

Dude, this is very clear! Much respect.

You can also use the abs() function to return the absolute value. It will always display a positive number so you could use

print(“The difference in insurance costs is: " + str(abs(diff)) + " dollars.”)

instead of using the if statement.

Thank you so much. You really made this look easy. My hat off to you.