Python Syntax - Medical Insurance Project - Calculation issue after bmi += 3.1

I am unable to figure out why the calculation I am getting does not match what the hint says it should be. This is only happening with the bmi calculation.

These are my initial variables:
age = 28
sex = 0 # 0 for female 1 for male
bmi = 26.2
num_of_children = 3
smoker = 0 #0 for non-smoker, 1 for smoker

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

For the bmi calculation we are supposed to reset age to 28 (it was 32 from the previous calculation) and increase bmi by 3.1.

Here is my code from the bmi section

age = 28
bmi += 3.1

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

change_in_insurance_cost = new_insurance_cost - insurance_cost

print(“The change in cost of insurance after increasing BMI by 3.1 is " + str(change_in_insurance_cost) + " dollars.”)

When I run this, the value I get is -384. The hint text states it should be 1147.0

It’s the exact same calculation as the age in the previous section so I don’t understand what is happening.

Python Syntax Project - Insurance

Can you post the code with proper formatting? It may reveal clues as to the nature of your issue.

To preserve code formatting in forum posts, see: [How to] Format code in posts

Here ya go. Sorry I didn’t post it that way the first time. Thank you for your assistance.

# create the initial variables below
age = 28
sex = 0 # 0 for female 1 for male
bmi = 26.2
num_of_children = 3
smoker = 0 #0 for non-smoker, 1 for smoker

# Add insurance estimate formula below
insurance_cost = 250 * age - 128 * sex
+ 370 * bmi + 425 * num_of_children
+ 24000 * smoker - 12500

print("This person's insurance cost is " + str(insurance_cost) + " dollars.")

# Age Factor
age += 4

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

change_in_insurance_cost = new_insurance_cost - insurance_cost

print("The change in cost of insurance after increasing the age by 4 years is " + str(change_in_insurance_cost) + " dollars")

# BMI Factor
age = 28
bmi += 3.1

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

change_in_insurance_cost = new_insurance_cost - insurance_cost

print("The change in cost of insurance after increasing BMI by 3.1 is " + str(change_in_insurance_cost) + " dollars.")

1 Like

The problem lies with the following two statements:
You wrote:

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

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

Since a single statement is broken up over multiple lines, so it is being interpreted as:

insurance_cost = 250 * age - 128 * sex

If you fix the lines to:

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

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

you should get the expected output.

1 Like

This just reinforces the fact that Python does pay attention to spacing and line breaks. Thank you!

1 Like

PEP 8 – Style Guide for Python Code

The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72).

The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Backslashes may still be appropriate at times. For example, long, multiple with-statements could not use implicit continuation before Python 3.10, so backslashes were acceptable for that case…


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

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

If using backslash, one has to be careful that there is no trailing whitespace after the \

2 Likes