Python Syntax: Medical Insurance Project

Hi. When I put in the values for new_insurance_cost it says that “sex” is not defined. new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500. Can you help me solve this issue. Thanks.

Is that all of the code?
Perhaps one of these threads on the same topic can help…?

https://discuss.codecademy.com/search?q=medical%20insurance%20syntax

This is the code that when I save it, it says sex is not defined

Yes, but there is code before that–variable definitions, etc… If you paste all your code perhaps someone will be better able to help debug it.

1 Like

create the initial variables below

age = 28

age += 4

Add insurance estimate formula below

Age Factor

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

print(age)

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

BMI Factor

BMI = 26.2

BMI += 3.1

new_insurance_cost - insurance_cost

Male vs. Female Factor

0

Extra Practice

num_of_children = 3

smoker = 0

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.”)

if you format your code, it’s easier to read. Use the “</>” button.

Why is there a 0 under “Male vs. Female Factor” ? Unless you changed the variable assignment?

sex = 0

 create the initial variables below
age = 28
age += 4

# Add insurance estimate formula below


# Age Factor
new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500
print(age) 
print( "The change in cost of insurance after increasing the age by 4 years is XXX dollars."   + str(change_in_insurance_cost) + " dollars " )

# BMI Factor
BMI = 26.2
BMI += 3.1
new_insurance_cost - insurance_cost

# Male vs. Female Factor
0

# Extra Practice
num_of_children = 3
smoker = 0
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.")

I put a 0 because she’s a female

Whenever you change the value of a variable, you have to do re-write it.
Ex:

sex = 0

You can’t just have “0” without assigning it to anything. See how you changed the value for BMI and age? You have to do the same for the other variables.

Also, I ran your code and I didn’t get an error. What error are you getting?

I didn’t have to change the value of female male factor, it just tells you to write a 1 or a 0 under it. So I’m confused. Also, I’m getting a name error.

You’re getting a name error b/c the variable isn’t assigned to anything.

You have to assign values to the variables In order for the insurance_cost formula to work…:

age = 28
sex = 0
bmi = 26.2
num_of_children = 3
smoker = 0

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

Otherwise, without an assignment, your calculation won’t work.

See the error message. It points to where the issue is:

The directions for step 10:
" On a new line of code, reassign the value of sex to 1 . A reminder that 1 identifies male individuals and 0 identifies female individuals."

Prior to that the variable was assigned the value of 0, right? You’re trying to see the difference in insurance costs when you change the values of the variables.

Your initial formula, insurance_cost takes in the initial values of those variables. Every time you change the value of the variables, whether it’s age, sex, bmi, or smoker, you’re then asked to create a new variable, new_insurance_cost & change_in_insurance_cost (or whatever it’s called) which still uses the formula that you initially created only the values of the variables has changed which results in a new cost.

2 Likes

I’m just copying from what the formula told me to write

Did you understand my explanation of the error?
I think you might need to go back over each of the 13 steps and figure out what is being asked and what your code is doing & why exactly you’re getting that error.

1 - 3:

# create the initial variables below
age = 28
sex = 0
bmi = 26.2
num_of_children = 3
smoker = 0

# 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
#print(age)
new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500
print(new_insurance_cost)

print("This person's new insurance cost with age increasing by 4 years is "   + str(new_insurance_cost) + " dollars")

I understand. Do I leave the formula the way it is and then add values to the variables?

Yep. The formula stays the same. You’re just updating the values of the variables and then re-running the formula and assigning it to a new variable, like you have written above (new_insurance_cost) . The formula will take whatever values you’ve assigned to the variables, age, sex, bmi, smoker, number_of_children.

(You already updated the age variable by 4 years from it’s initial value and bmi by 3.)