Python Loops: Medical Insurance Estimates vs. Costs Project

Hi I am stuck on the project
Python Loops: Medical Insurance Estimates vs. Costs Project
https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-loops/projects/ds-python-loops-project
Use a for loop to iterate through actual_insurance_costs and add each insurance cost to the variable total_cost .

i looke at hint total_cost += insurance_cost, I dont understand this += why use it here?

That is one of the group of assignment operators which augments the current value to build up a tally (total).

a = 6
a += 7
print (a)    #  13

Thanks!

my code is keep giving File “script.py”, line 7
total_cost += isurance_cost
^
IndentationError: expected an indented block

1 Like

Since that line is in a loop body, it needs to be indented.

Aside

One gets the feeling you may have opted to challenge yourself by jumping right into a project, before completing the Learn Python 3 track. You’ll find that course is most essential to get ourselves up to speed. Suggest pause this project and look for that course in your dashboard. Spend the holidays working on it and you’ll find the projects a little easier to think through. They are intentionally written to let us see how much we are picking up and understanding.

Sorry only replied to you now, I am following the course data science step by step , but really find it difficult to understand the questions

Suggest take a deep dive into the Learn Python 3 track to reinforce the core of the language.

Can anyone explain why I’m getting this error? Looks to me like my code is the exact same as in the Hint provided.


image

Double check the print() above your for loop.

1 Like

Thanks! I see I left the closed parenthesis off… but why would it indicate the error is on line 12?

1 Like

Sometimes error messages are very specific and sometimes they refer to lines of code above.

2 Likes

Hi people, first time here in forums :wink:

I’m stuck in this project too, but in the last topic:

" If you’d like extra practice with Python loops, here are some suggestions to get you started:

  • Convert the first for loop in the code to a while loop.
  • Modify the second for loop so that it also calculates how far above or below the average the estimated insurance cost is."

The first loop is:

names = [“Judith”, “Abel”, “Tyson”, “Martha”, “Beverley”, “David”, “Anabel”]
estimated_insurance_costs = [1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0]
actual_insurance_costs = [1100.0, 2200.0, 3300.0, 4400.0, 5500.0, 6600.0, 7700.0]

total_cost = 0
for actual_costs in actual_insurance_costs:
total_cost += actual_costs
average_cost = total_cost/len(actual_insurance_costs)
print(“Average Insurance Cost: " + str(average_cost) + " dollars.”)

There is no sentence that is a condicional sentence…can someone help me ?

the second loop:

 for i in range(len(names)):
  name = names[i]
  insurance_cost = actual_insurance_costs[i]
  print("The insurance cost for " + name +  " is " + str(insurance_cost ) +  " dollars.")
  if insurance_cost > average_cost:
    print("The insurance cost for " + name + " is above average.")
  elif insurance_cost < average_cost:
    print("The insurance cost for " + name + " is below average.")
  else:
    print("The insurance cost for " + name + " is equal to the average.")