Python Functions: Medical Insurance Project Step 13 Interpreter Question

So I have a question regarding how Python interprets functions. Here is my code:

# Create calculate_insurance_cost() function below: def calculate_insurance_cost(name, age, sex, bmi, num_of_children, smoker): estimated_cost = insurance_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 # Initial variables for Maria # 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) # Initial variables for Omar # 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) # Estimate for Cody's insurance cost cody_insurance_cost = calculate_insurance_cost(name="Cody", age=37, sex=1, bmi=30, num_of_children=0, smoker=0) # Here is my other version where I can reuse the return from LINE 5, # essentially letting me reuse the code without rewrites def difference_in_insurance_cost_print_plus_return(patient_one, patient_two): difference = abs(patient_one - patient_two) print("The difference in insurance cost between $" + str(patient_one) + " and $" + str(patient_two) + " is $" + str(difference) + ". This function uses original function.") maria_vs_omar = difference_in_insurance_cost_print_plus_return(maria_insurance_cost, omar_insurance_cost) maria_vs_cody = difference_in_insurance_cost_print_plus_return(maria_insurance_cost, cody_insurance_cost) cody_vs_omar = difference_in_insurance_cost_print_plus_return(cody_insurance_cost, omar_insurance_cost)

My question is: how does the Python interpreter know to ignore the original print statement used in the first function? It prints everything just fine for the Maria, Omar, and Cody variables, but it ignores them when I reuse them in the second function.

At the same time, it knows to use the return statements as an argument for the second function. Why didn’t the interpreter use both the print and the returns? Can I use both the return and print (my guess is no or if yes, some code rewrite will be required)?

I am just confused because the only other language I kind of understand is JavaScript and I don’t recall being able to do this type of stuff. Maybe you can and I just don’t remember.

Thanks to anyone who chimes in!

Couple of things here. It’s not ignoring the print statement. It’s just not getting invoked during the second function call.

maria_vs_omar = difference_in_insurance_cost_print_plus_return(calculate_insurance_cost(name="Maria", age=28, sex=0, bmi=26.2, num_of_children= 3, smoker=0), omar_insurance_cost)

would definitely make it print the first function’s print statement (as an example only, this is very awkward code to write in one line)

Also your function is not returning anything because there’s no return statement.
So for

maria_vs_cody = difference_in_insurance_cost_print_plus_return(maria_insurance_cost, cody_insurance_cost)

maria_vs_cody is going to be assigned None.
If you add return difference this will now be assigned a value.

2 Likes

Thnx for your help! I have some more studying to do!