Help!Medical Insurance Project

Hi folks,

Been having problem with age variable error.Kindly enlighten me on the problem to fix. Cheers.

Here’s my code:

create the initial variables below

def insurance_cost(age,sex,bmi,num_of_children,smoker):

age = 28

sex = 0

bmi = 26.2

num_of_children = 3

smoker = 0

Add insurance estimate formula below

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

print(“This person’s insurance cost is”+""+insurance_cost+""+“dollars.”)

Error message:

Traceback (most recent call last):
File “script.py”, line 10, in
insurance_cost=250age-128sex+70bmi+425num_of_children+24000*smoker-12500
NameError: name ‘age’ is not defined

Error:

Hello @aaronong3534137927.

Before we continue, could you format your cost as per this post? It’d make it easier for everyone to understand it and preserve the code indentation.

Thank you.

1 Like

This code is confusing. Without any indentation, and Python uses indentation to show what is in and not in a function, I can’t tell what is going on.

My suspicion is that the function (insurance_cost) is supposed to be called with the arguments being the variables you created (age, sex, etc). The function is then supposed to return the total insurance cost based on the formula.

Instead, the function looks like the function contains all the variables, but it doesn’t contain the formula or return anything. It seems like it should be the other way around. The variables should be outside the function and the function should contain the formula and return a value that can be printed.

1 Like

NameError: name ‘age’ is not defined

Even without function, there’s error:

>>> age = 28
>>> sex = 0
>>> bmi = 26.2
>>> num_of_children = 3
>>> smoker = 0
>>> insurance_cost= 250age-128sex+70bmi+425num_of_children+24000*smoker-12500
  File "<stdin>", line 1
    insurance_cost= 250age-128sex+70bmi+425num_of_children+24000*smoker-12500
                       ^
SyntaxError: invalid syntax

In other environment/IDE:

    insurance_cost= 250age-128sex+70bmi+425num_of_children+24000*smoker-12500
                       ^
SyntaxError: invalid syntax

They all points to the same error 250age
This is not acceptable in Python. Do you mean 250 * age ?

1 Like

The * is in the original code. The * is missing in this post because the markdown parser thinks it is formatting.

I believe it is supposed to look like this.

def insurance_cost(age,sex,bmi,num_of_children,smoker):
    return (250 * age) - (128 * sex) + (70 * bmi) + (425 * num_of_children) + (24000 * smoker) - 12500

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

total_insurance_cost = insurance_cost(age, sex, bmi, num_of_children, smoker)
print("This person’s insurance cost is", total_insurance_cost, "dollars.")

Also, the use of the + in the print would return an error since you can’t concatenate string and numbers. You need to use the comma instead.

1 Like

Ahh… that’s right! Can see that certain portions are in italics. :rofl:

2 Likes

Cheers everyone for your inputs.

Greatly appreciated!

For some reason it still returns this error:


total_insurance_cost=return (250 * age) - (128 * sex) + (70 * bmi) + (425 * num_of_children) + (24000 * smoker) - 12500
    ^
IndentationError: unexpected indent

You have it right. It should look like this.

def insurance_cost(age,sex,bmi,num_of_children,smoker):
    return (250 * age) - (128 * sex) + (70 * bmi) + (425 * num_of_children) + (24000 * smoker) - 12500

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

total_insurance_cost = insurance_cost(age, sex, bmi, num_of_children, smoker)
print("This person’s insurance cost is", total_insurance_cost, "dollars.")

Cheers buddy. A lifesaver you are.

1 Like