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