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


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))
    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]

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?


while i<=len(actual_insurance_costs):

The actual code has the proper indentation.

1 Like

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

while i<len(actual_insurance_costs):

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 isurance_cost in actual_insurance_costs: # total_cost += isurance_cost i = 0 while i < len(actual_insurance_costs): total_cost += actual_insurance_costs[i] i += 1 print("Total insurance cost :" + str(total_cost)) average_cost = total_cost / len(actual_insurance_costs) print("Averag insurance cost :" + str(average_cost)) 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 avobe average") elif insurance_cost < average_cost: print("The insurance cost for " + name + " is below average") else: print("The insurance cost for " + name + " is equal average") updated_estimated_costs = [ es_ins_cost * 11 / 10 for es_ins_cost in estimated_insurance_costs ] print(updated_estimated_costs)

For some reason, the logic isn’t matching with the hints provided. I’ve made different attempts to change the answer, based on how the code is written out, but this doesn’t seem to work.

In the future please consider starting a new thread instead of reviving a solved topic.

I think you have statements which are split onto more than one line actually creating two separate statements.

For example in the following x would equal 6, x = 1 + 2 + 3.

x = 1 + 2 + 3
+ 4 + 5  # this line is a different expression which is never assigned!
Out: 6

Your lines cannot be split randomly, make sure it’s a single line for now.

1 Like