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.

1 Like

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.

2 Likes

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.")

I’m also stuck there. Can someone explain how to transform these loops? So far I thought I’d understood the difference and usage of for and while loops but I can’t seem to figure this one out.

Would you like to share your attempted solution? It may reveal your thinking process and where you may be hitting a stumbling block.

Or, are you having trouble getting started altogether?

With a while loop, I would think you would want to use the index to iterate over and access the elements of the list. If we just want to access elements of a list, then a for loop is usually more convenient. If we want to modify/re-assign values of the elements, then a while loop making use of the index can allow us to make direct changes (a for loop iterating over the index would be better, but both the while and the for loop can accomplish the task, though we have to keep track of some extra details in the while loop).

Hello :slight_smile:

I would just like to get some clarity on the following:

actual_insurance_costs = [1100.0, 2200.0, 3300.0, 4400.0, 5500.0, 6600.0, 7700.0] total_cost = 0 for cost in actual_insurance_costs: total_cost += cost print(total_cost)

result of the above is: 30800.0

If I switch total cost += cost around to cost += total_cost

actual_insurance_costs = [1100.0, 2200.0, 3300.0, 4400.0, 5500.0, 6600.0, 7700.0] total_cost = 0 for cost in actual_insurance_costs: cost += total_cost print(total_cost)

the result is: 0

I am not understanding why it doesn’t work both ways?

In both the code snippets, you are initializing total_cost as 0.

The loop variable cost is being used to iterate over the actual_insurance_costs list. That means, in the first iteration cost will be assigned the value 1100.0, in the second iteration it will be assigned the value 2200.0 and so on till the whole list is traversed.

In the first snippet, in every iteration of the loop the following statement is executed:

total_cost += cost

Since numbers are an immutable data type, so using the += operator can be considered as being equivalent to:

total_cost = total_cost + cost

In every iteration of the loop, you add the number assigned to cost to the existing total_cost and store the result in total_cost. Once the loop finishes, total_cost holds the value 30800.0

In the second snippet, in every iteration of the loop the following statement is executed:

cost += total_cost

which can be considered as being equivalent to:

cost = cost + total_cost

In the first iteration, total_cost is 0 and cost is 1100.0. After executing the above statement, the result of the addition will be 1100.0 which will be assigned to the cost variable (as a side note, the original list will not be mutated because cost just holds the value and not the reference to the list element, but I digress).

In the second iteration, total_cost is 0 and cost will be 2200.0 (because the second element of the list is 2200.0. The value assigned to cost in the last iteration will be overwritten). After executing cost = cost + total_cost, the value assigned to cost will be 2200.0

Once the loop finishes, cost will hold the value 7700.0 (since that is the last element of the list). total_cost will still be 0. When you print this variable, print(total_cost), the output will be 0.

1 Like

Hello,

Here is the solution for the Python Loops: Medical Insurance Estimates vs. Costs Project including “Extras”

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]

# Add your code here
total_cost = 0

for cost in actual_insurance_costs:
  total_cost += cost

average_cost = total_cost / len(actual_insurance_costs)
print("Average Insurance Cost: " + str(average_cost) + " dollars.")


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.")

updated_estimated_cost = [cost * 11/10 for cost in estimated_insurance_costs]
print(updated_estimated_cost)

# Extra

# Convert the first for loop in the code to a while loop
total_cost = 0
index = 0

while index < len(actual_insurance_costs):
  total_cost += actual_insurance_costs[index]
  index += 1

print(total_cost)

# Modify the second for loop so that it also calculates how far above or below the average the estimated insurance cost is.
for i in range(len(names)):
  name = names[i]
  insurance_cost = actual_insurance_costs[i]

  if insurance_cost > average_cost:
    higher_cost = insurance_cost - average_cost
    print("The insurance cost for " + name + " is " + str(higher_cost) + " above average.")

  elif insurance_cost < average_cost:
    lower_cost = average_cost - insurance_cost
    print("The insurance cost for " + name + " is " + str(lower_cost) + " below average.")

  else:
    print("The insurance cost for " + name + " is equal to the average.")

It took time for me to figure out how to convert the first for loop into while loop.

Appreciate any feedback

Happy Coding!
Cheers! :+1:

2 Likes

Thank you so much for your “extra” help.

1 Like