Python code not showing output

Hi Python community,

I’ve tried coding the health insurance cost project but couldn’t get an ouput. On the flipside no errors too. Thanks in advance for any help rendered.

This is the code:

def calculate_insurance_cost(age,sex,bmi,num_of_children,smoker,name):

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

maria_insurance_cost = calculate_insurance_cost( 28,0,26.2,3,0, name=“Maria”)

omar_insurance_cost = calculate_insurance_cost(35,1,22.2,0,1, name=“Omar”)

return maria_insurance_cost,omar_insurance_cost

print("The estimated insurance cost for " + name + "is " + str(maria_insurance_cost))

print("The estimated insurance cost for " + name+ "is " + str(omar_insurance_cost))

Can you please format your code like described here:

How do I format code in my posts?

so we can see the indention of your code

1 Like

Assuming this is your actual indentation (raw text of your post)

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
  maria_insurance_cost = calculate_insurance_cost( 28,0,26.2,3,0, name="Maria")
  omar_insurance_cost = calculate_insurance_cost(35,1,22.2,0,1, name="Omar")
  
  return maria_insurance_cost,omar_insurance_cost
   
  print("The estimated insurance cost for " + name + "is " + str(maria_insurance_cost))
  print("The estimated insurance cost for " + name+ "is " + str(omar_insurance_cost))

Were do you call your function? And when you do, check the order of your print()s and return. A return exits the function, so anything after that won’t be executed.

1 Like

Thanks 8-bit-gaming for your reply.

Can you provide specifics? Should the return go to a different place?

Correct me if I’m wrong to call this function I need this line:

calculate_insurance_cost(28,0,26.2,3,0) 

To get Maria’s insurance cost.

I’m a bit lost at the moment.

Hmm, I missed earlier that you are calling you function within your function.

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
  maria_insurance_cost = calculate_insurance_cost( 28,0,26.2,3,0, name="Maria")

I’m not certain that’s what you want though, as this causes an infinite loop of the function calling itself until you run out of memory and the program crashes.

It might be good to step back and review a couple function basics. The two biggest points I would say are that return is the last line of a function when it runs so currently your print()s never do. Then you shouldn’t call a function from a function without some logic to keep it from becoming an infinite loop.

I don’t know for certain how the project has things set, but I would guess they want you to return estimated_cost from the function and then have your function calls and print statements elsewhere.