U.S. Medical Insurance Costs (Portfolio Project)

I am working on the Insurance Portfolio Project and I believe I have been doing pretty well but there is something wrong and after a couple hours I can’t seem to figure out what.
I set a variable patient_info equal to the class Patients and plugged in the lists from the data as the parameters for Patients (located at the bottom of the code) but am getting this error.
Could have other errors too that aren’t throwing error messages so if you see something please bring it to my attention as well! Thank you in advance!

ps: (not sure if I’m using the line break function (’\n’) correctly)

import csv
import statistics


ages = []
sexes = []
bmis = []
num_children = []
smoker_status = [] 
regions = []
insurance_charges = []


def load_lists(lst, file, column):
    with open(file) as insurance_csv:
        insurance_reader = csv.DictReader(insurance_csv)
        
        for row in insurance_reader:
            lst.append(row[column])
        return lst

ages = load_lists(ages, 'insurance.csv', 'age')
sexes = load_lists(sexes, 'insurance.csv', 'sex')
bmis = load_lists(bmis, 'insurance.csv', 'bmi')
num_children = load_lists(num_children, 'insurance.csv', 'children')
smoker_status = load_lists(smoker_status, 'insurance.csv', 'smoker')
regions = load_lists(regions, 'insurance.csv', 'region')
insurance_charges = load_lists(insurance_charges, 'insurance.csv', 'charges')


   
class Patients:
    def __init__(self, patient_age, patient_sex, patient_bmi, patient_num_children, patient_smoker_status, patient_regions, patient_insurance_charges):
        self.patient_age = patient_age
        self.patient_sex = patient_sex
        self.patient_bmi = patient_bmi
        self.patient_num_children = patient_num_children
        self.patient_smoker_status = patient_smoker_status
        self.patient_regions = patient_regions
        self.patient_insurance_charges = patient_insurance_charges

    
    def average_age(self, patient_age):
        sum_age = 0
        for age in patient_age:
            sum_age += int(age)
            avg_age = (sum_age)/(len(patient_age))
        return ('The average age of all patients is: ', str(round(avg_age, 2)))

    def analyze_sexes(self, patient_sex):
        male = 0
        female = 0
        for sex in patient_sex:
            if sex == 'male':
                male += 1
            else:
                female += 1
        return 

    def region_analysis(self, patient_regions):
        south_west = 0
        south_east = 0
        north_west = 0
        north_east = 0
        for place in patient_regions:
            if place == 'southwest':
                south_west +=1
            if place == 'southeast':
                south_east +=1
            if place == 'northwest':
                north_west +=1
            if place == 'north_east':
                north_east += 1
        print('There are ' + str(south_west) + ' patients in the southwest')
        print('There are ' + str(south_east) + ' patients in the southeast')
        print('There are ' + str(north_west) + ' patients in the northwest')
        print('There are ' + str(north_east) + ' patients in the northeast')

    def average_cost_analysis(self, patient_insurance_charges):
        sum_cost = sum(patient_insurance_charges)
        avg_cost = sum_cost / len(patient_insurance_charges)
        return ('The average insurance cost is: ' + str(avg_cost))
        
    def smoker_v_nonsmoker(self, patient_smoker_status):
        smokers = 0
        nonsmokers = 0
        for smoke in patient_smoker_status:
            if smoke == 'yes':
                smokers += 1
            else:
                nonsmokers += 1
        return (f'The number of smokers is: ' + str(smokers),'\n' 'The number of non-smokers is: ' + str(nonsmokers))

    def kids_v_nokids(self, patient_num_children):
        no_kids = 0
        kids = 0
        for kid in patient_num_children:
            if int(kid) == 0:
                kids += 1
            else:
                no_kids += 1
        return (f'There are ' + str(no_kids) + ' patients with no kids.\n', 'There are ' + str(kids) + ' patients with kids.')

    def dictionary(self):
        self.patient_dict = {}
        self.patient_dict['age'] = [int(ages) for ages in self.patient_age]
        self.patient_dict['sex'] = self.patient_sex
        self.patient_dict['bmi'] = self.patient_bmi
        self.patient_dict['children'] = self.patient_num_children
        self.patient_dict['smoker'] = self.patient_smoker_status
        self.patient_dict['region'] = self.patient_regions
        self.patient_dict['charges'] = self.patient_insurance_charges

        return self.patient_dict









patient_info = Patients(ages, sexes, bmis, num_children, smoker_status, regions, insurance_charges)

patient_info.average_age(ages)
# patient_info.average_cost_analysis()


Your error suggests your method is missing a positional argument. Check the definition of this function and work out whether you need to alter your function call to include the missing argument or whether you want to alter the definition itself.

right, I solved the error by getting rid of the parameters in all of my methods and instead using self.parameter_needed when using them in my methods. like this,

def average_cost_analysis(self):
        sum_cost = sum(self.patient_insurance_charges)
        avg_cost = sum_cost / len(self.patient_insurance_charges)
        return ('The average insurance cost is: ' + str(avg_cost))

Still don’t get any output when I call the the instance I created along with a method. Any further advice to why I can’t get any output when running this code would be greatly appreciated! thank you

What are you doing the object you return? Are you looking to perhaps print to the console or something?

thats correct. So I should just be doing it like this? I originally did have a print statement but I think it wasnt working so I ended up checking the solution. They only use return statements and call the class and method with the instance like I did, so that confused me.
However, when I have it like this it is working now! Not sure what I was doing before but thank you for walking through that with me.
So I always have to have a print() to get it to the console right? confused how they didn’t have that in the solution…
I took a little break from codecademy so am a little rusty

    def smoker_v_nonsmoker(self):
        smokers = 0
        nonsmokers = 0
        for smoke in self.patient_smoker_status:
            if smoke == 'yes':
                smokers += 1
            else:
                nonsmokers += 1
        print(f'The number of smokers is: ' + str(smokers),'\n' 'The number of non-smokers is: ' + str(nonsmokers))
        return smokers, nonsmokers
1 Like

Yes, if you wanted to output your variables to the console you should ask for it. The majority of functions use return and don’t contain print which is generally more flexible.

If you still wanted to print the output of a function then print(myfunction(arguments)) outside the function is probably the best way to approach it.

I did try print(patient_info.average_cost_analysis()) and it returned ‘None’ but it is working with my print statement in the method so I am fine with that for now. Thanks for your help! have a great day