Python Wizards Unite! Function call help plz

I have a quick questions about one of the projects in my skills path. I posted all of the code bellow. As you can see I have placed my function calls inside of my variables. This code works and I do not understand how. I never actually call the variable, so how is the function call working?


▼ def calculate_insurance_cost (age, sex, bmi, num_of_children, smoker, name):
         estimated cost = 250*age-128*sex
          +370*bmi+425*num_of_children+24000*smoker-12500
          print("The estimated insurance cost for " + name +" is " + str(estimated_cost) + " 
          dollars")
          return estimated cost
maria_insurance_cost calculate_insurance_cost (age 28,sex = 0, bmi = 26.2,num_of_children = 3,smoker = 0,name "Maria")

omars_insurance_cost = calculate_insurance_cost(age = 35, sex = 1, bmi = 22.2, num_of_children = 0, smoker =1, name = "Omar")

Lars_insurace_cost = calculate_insurance_cost (age = 64, sex = 1, bmi = 22.2, num_of_children = 12, smoker = 1, name="Lars")`

You have return estimated cost, which exits the function and returns a value to the caller.
You’ve created a variable, maria_ins_cost and set it equal to calling the function…which passed arguments–age, sex, bmi, etc. through the function and the estimated cost was calculated and then returned.

1 Like

Thank you for explaining everything, this really helps!

1 Like

Arguments must be values, not assignments.

maria = foo (28, 0, 26.2, 3, 0, "Maria")

The arguments are unpacked and assigned to the positional parameters of the function.

I think I understand what you are explaining. I used keyword arguments, instead I should have input just the values of my positional arguments?

1 Like

(I think) this is how it’s asked to do in the lesson in order to pass. (my code was the same). Either works.

Summary
def calculate_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("The estimated insurance cost for " + name + " is: "   + str(estimated_cost) + " dollars. ")

  return estimated_cost

maria_insurance_cost = calculate_insurance_cost(name = "Maria", age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0)

maria_insurance_cost = calculate_insurance_cost("Maria", 28, 0, 26.2, 3, 0)


>>The estimated insurance cost for Maria is: 5469.0 dollars. 
The estimated insurance cost for Maria is: 5469.0 dollars. 
2 Likes

Correct. We can only make assignments in the parameter list, not the argument list. (Going to check this, just to be sure.)

Dang! How did I ever forget about this? Keyword arguments are perfectly valid, but, if the lesson is not being accepted, then it may be the issue in this instance.

Be sure to assign the return value using =, and if doing so, let the printing take place at the caller, rather than inside the function, or create a printing function on the side. Functions should be kept simple and perform a set task, imho.

1 Like