Try and except error handling

Hi all,

I am currently working through the ‘Python Control Flow: Medical Insurance Project’ and I am trying to add in some error control. Specifically for the analyze_smoker part of the code, if it works I would then do the same for analyze_bmi.

One of the inputs is smoker, which can be 0 or 1 for the program to work. I am attempting to add in an error code so that, if someone puts yes/no the program will state “Please use 1 for smoker, and 0 for non-smoker.”

However, I keep getting NameError anyways.

If someone could spare a glance i would appreciate it!

# Add your code here
def analyze_smoker(smoker_value):
  
    if smoker_value == 1:
     print("To lower you cost, you should consider quitting smoking.")
    else:
      print("Smoking is not an issue for you.")




def analyze_bmi(bmi_value):
  if bmi_value > 30:
    print("Your BMI is in the obese range. To lower your cost, you should significantly lower your BMI.")
  if bmi_value >= 25 and bmi_value <=30:
    print("Your BMI is in the overweight range. To lower your cost, you should lower your BMI.")
  if bmi_value >=18.5 and bmi_value <25:
    print("Your BMI is in a healthy range.")
  if bmi_value < 18.5:
    print("Your BMI is in the underweight range. Increasing your BMI will not help lower your cost, but it will help improve your health.")

# Function to estimate insurance cost:
def estimate_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
  print(name + "'s Estimated Insurance Cost: " + str(estimated_cost) + " dollars.")
 
  try:
    analyze_smoker(smoker)
  except NameError:
    print("Please use 1 for smoker, and 0 for non-smoker.")

  analyze_bmi (bmi)

  return estimated_cost
 
# Estimate Keanu's insurance cost
keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, sex = 1, bmi = 26.2, num_of_children = 3, smoker = yes)


but the name error comes from here:

keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, sex = 1, bmi = 26.2, num_of_children = 3, smoker = yes)

yes is a undefined/unnamed variable. Should be a string

Also within your analyze_smoker:

def analyze_smoker(smoker_value):
  
    if smoker_value == 1:
     print("To lower you cost, you should consider quitting smoking.")
    else:
      print("Smoking is not an issue for you.")

the exception is never thrown? And I would argue, that ValueError might be more appropriate.

Hi Stetim,

I changed smoker = yes to smoker = “yes” so that it is now a string.

Not sure what ‘exception is never thrown’, is thrown the same as just getting the exception?

I have however now put keanu_insurance_cost in a try/except block, which seems to do the job. I had (I think) corrected anaylzer, but I was getting error due to using + with a float/int and string.

My code now looks like this, and seems to generate the correct error response.

# Add your code here
def analyze_smoker(smoker_value):
  
    if smoker_value == 1:
     print("To lower you cost, you should consider quitting smoking.")
    else:
      print("Smoking is not an issue for you.")




def analyze_bmi(bmi_value):
  if bmi_value > 30:
    print("Your BMI is in the obese range. To lower your cost, you should significantly lower your BMI.")
  if bmi_value >= 25 and bmi_value <=30:
    print("Your BMI is in the overweight range. To lower your cost, you should lower your BMI.")
  if bmi_value >=18.5 and bmi_value <25:
    print("Your BMI is in a healthy range.")
  if bmi_value < 18.5:
    print("Your BMI is in the underweight range. Increasing your BMI will not help lower your cost, but it will help improve your health.")

# Function to estimate insurance cost:
def estimate_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
  print(name + "'s Estimated Insurance Cost: " + str(estimated_cost) + " dollars.")
 
  try:
    analyze_smoker(smoker)
  except TypeError:
    print("Please use 1 for smoker, and 0 for non-smoker.")

  analyze_bmi (bmi)

  return estimated_cost
 
# Estimate Keanu's insurance cost
try:
  keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, sex = 1, bmi = 26.2, num_of_children = 3, smoker = "Yes")
except TypeError:
    print("Please use 1 for smoker, and 0 for non-smoker.")

#testing with 1 for smoker.
try:
  keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, sex = 1, bmi = 26.2, num_of_children = 3, smoker = 1)
except TypeError:
    print("Please use 1 for smoker, and 0 for non-smoker.")

No. The NameError you got because of this:

keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, sex = 1, bmi = 26.2, num_of_children = 3, smoker = yes)

Means that somewhere along the line, an exception was raised/thrown by python. Because you tried to do something (using an unnamed variable, which is why you got a NameError)

try/catch is merely handling of a raised/thrown exception.

we can also throw/raise exceptions if we like:

https://docs.python.org/3/tutorial/errors.html#raising-exceptions