Python Control Flow: Medical Insurance Project

Hay guys can anybody help me to solve this code

# Add your code here
 
# Function to estimate insurance cost:
def estimate_insurance_cost(name, age, sex, num_of_children, smoker):
  estimated_cost = 400*age - 128*sex + 425*num_of_children + 10000*smoker - 2500
  print(name + "'s Estimated Insurance Cost: " + str(estimated_cost) + " dollars.")
  analyze_smoker(smoker = 1)
  return estimated_cost
 
# Estimate Keanu's insurance cost
keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, sex = 1, num_of_children = 3, smoker = 1)

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

alvin_insurance_cost = estimate_insurance_cost(name = 'Alvin', age = 29, sex = 0, num_of_children = 1, smoker = 1 )

So the problem is that I want to create a function to call analyze_smoker() inside the setimate_insurance_cost. But the error message shows that my function is not defined

There are two issues:

  • First issue. The function call
keanu_insurance_cost = estimate_insurance_cost(name = 'Keanu', age = 29, 
sex = 1, num_of_children = 3, smoker = 1)

has been made after you have defined estimate_insurance_cost function, but before analyze_smoker function’s definition. The Python interpreter is going to read your code from top to bottom. When it reaches the function call, then estimate_insurance_cost will be called with the arguments provided by you. During the execution of estimate_insurance_cost, when it reaches the call to analyze_smoker, an error will be thrown because analyze_smoker hasn’t been defined yet.

If you move your function call below the definitions of both functions, then your function call will work.

It doesn’t matter whether you define analyze_smoker first and then estimate_insurance_cost OR vice versa. Both will work if your function call is below both function definitions.

# THIS WILL WORK 
def estimate_insurance_cost(...):
    return ...

def analyze_smoker(...):
    return ...

keanu_insurance_cost = estimate_insurance_cost(...)
alvin_insurance_cost = estimate_insurance_cost(...)

# THIS WILL ALSO WORK 
def analyze_smoker(...):
    return ...

def estimate_insurance_cost(...):
    return ...

keanu_insurance_cost = estimate_insurance_cost(...)
alvin_insurance_cost = estimate_insurance_cost(...)
  • Second issue. In the estimate_insurance_cost function, you are making the call:
# You wrote:
analyze_smoker(smoker = 1)

# Change it to:
analyze_smoker(smoker)

When you made the function call,

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

you already provided a value for smoker. During execution of estimate_insurance_cost, the provided value will be assigned to the parameter smoker. You can then use smoker to pass this value as an argument to analyze_smoker. If you make the function call analyze_smoker(smoker = 1), an error will be thrown about an unexpected keyword argument, because the parameter of analyze_smoker is smoker_status and not smoker. By omitting the keyword and simply making the call analyze_smoker(smoker), the value assigned to smoker will be passed as the argument and will be assigned to smoker_status.

If you still find something confusing, share your thoughts.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.