US Medical Insurance Costs Class Method Help

Hi all,

I built this project using a class and then defining class methods. One of the analysis I am trying to perform is average cost by region. Here is the relevant code:

Class Method:

 # Average cost by region*
    def avg_cost_by_region(self):
        southwest = 0
        northwest = 0
        southeast = 0
        northeast = 0
        counter = 0
        while counter <= len(self.patient_regions):
            if self.patient_dict['region'][counter] == 'southwest':
                southwest += float(self.patient_dict['charges'][counter])
                counter += 1
            elif self.patient_dict['region'][counter] == 'northwest':
                northwest += float(self.patient_dict['charges'][counter])
                counter += 1
            elif self.patient_dict['region'][counter] == 'southeast':
                southeast += float(self.patient_dict['charges'][counter])
                counter += 1
                northeast += float(self.patient_dict['charges'][counter])
                counter += 1
        print(f'''The average cost by region is:
        Southwest: ${southwest}
        Northwest: ${northwest}
        Southeast: ${southeast}
        Northeast: ${northeast}''')

Error Message when I try to call it (patients_info is the instantiated class object):

IndexError                                Traceback (most recent call last)
Input In [40], in <cell line: 1>()
----> 1 patients_cost_by_region = patients_info.avg_cost_by_region()

Input In [31], in PatientsInfo.avg_cost_by_region(self)
     84 counter = 0
     85 while counter <= len(self.patient_regions):
---> 86     if self.patient_dict['region'][counter] == 'southwest':
     87         southwest += float(self.patient_dict['charges'][counter])
     88         counter += 1

IndexError: list index out of range

I’ve called a direct reference to a random index within the dictionary and it returns what I’d expect. For example:

This returns “southwest” which is what I expected.

counter = 0

I’m not sure why I am getting this error. I believe my class method should be able to take a reference to the class method I used to create the dictionary. Any thoughts on why the script thinks I’m out of range when it was only on the first call?