Here is my Python Loops: Medical Insurance Project

Hi everyone, this is the second project post on forums. Any advice is welcome. :smiley:

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]
actual_insurance_costs_copy = [1100.0, 2200.0, 3300.0, 4400.0, 5500.0, 6600.0, 7700.0]

# Add your code here
total_cost = 0

#[for loop]:
#for cost_a in actual_insurance_costs:
  #total_cost += cost_a

#To use [while loop] and function .pop add up total_cost, have to use the actual_insurance_costs's copy, or the rest of code can't literate an empty list.
while len(actual_insurance_costs_copy) > 0:
  a_cost = actual_insurance_costs_copy.pop()
  total_cost += a_cost

print(total_cost)

average_cost = total_cost / len(actual_insurance_costs)
print('Average Insurance Cost: {0} dollars.'.format(average_cost))

for i in range(len(names)):
  name = names[i]
  insurance_cost = actual_insurance_costs[i]
  print('The insurance cost for {0} is {1} dollars.'.format(name, insurance_cost))
  average_actual_cost = sum(actual_insurance_costs) / len(actual_insurance_costs)
  if insurance_cost > average_actual_cost:
    upper_different = insurance_cost - average_actual_cost
    print('The insurance for {0} is ${1} costly than average.'.format(name, upper_different))
  elif insurance_cost < average_actual_cost:
    lower_different = average_actual_cost - insurance_cost
    print('The insurance for {0} is ${1} cheaper than average.'.format(name, lower_different))
  else:
    print('The insurance cost for {0} is equal to the average.'.format(name))

updated_estimated_costs = [up_cost * 11 / 10 for up_cost in estimated_insurance_costs]
print(updated_estimated_costs)

One more question. Is there any other way to build that while loop? Cause I have to use a list copy of actual_insurance_costs to do that.

You seem to have a valid solution in the code but it’s edited out. Either way using references instead of a copy would be easier.

Alternatively have a look at the built-in sum-

Thanks for replying.
Those are my own code, and I mean to use another better while loop to build that while loop in the code, cause I think to use a list copy to do that might not efficient in a real task.
That for loop which I edited out was a solution to another task.
Python Loops Medical_Insurance_Project.ipynb.txt (3.8 KB)

You’d be making a bit harder on yourself with a while loop (a for loop is much simpler for this task) but it can definitely be done without copying a list. Instead of copying it why not access it by index so actual_insurance_costs[0] and so on until the end of the list. You’d need some form of counter and the length of the list to do so.

Thanks for replying. :smiley:
Could you do more detail explain about that quote? :thinking:

Say you have a list with three elements, lst = ['a', 'b', 'c']. You can access each of those elements in turn with their index, lst[0] refers to 'a', lst[1] refers to 'b' and lst[2] refers to 'c'.

If you got the length of this list and then iterated with range over the length then you could access each element in turn by their index allowing a simple way to count a total without copying an entire list.

Perhaps more importantly, you already have code that does this. If you’re unsure about the code you already have then take a step back sit down and learn it properly, run through the loops sections again if necessary. It’s something you will definitely need to know as the course progresses and it is pretty much bread and butter syntax (essential). You’ll do yourself no favours by just glossing it over.

Thanks for your suggestion.
I think unlike for loop, to make while loop work, it must have some kind of condition. I don’t know under which condition it can iterate through a list.

After checking the Python loop section, there is one page 7/11 about while loop, and you could notice that is the same way in my code by using .pop function to add up the total cost.

Perhaps it would be better to do some digging from other sources. Thanks for your time.

1 Like

Hello I’m trying to work out the Extra section for the same project - converting a “for” loop to a “while” loop for the first loop statement but I seem can’t have it work. Can someone take a look?

total_cost=0

while i<=len(actual_insurance_costs):
print(total_cost)
total_cost+=actual_insurance_costs[i]

The actual code has the proper indentation.

I actually solved my own question - here is the code that worked

total_cost=0
i=0
while i<len(actual_insurance_costs):
print(total_cost)
total_cost+=actual_insurance_costs[i]
i=i+1