You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility
I’m relatively new to Python and have been experiencing the following NameError when I try to create an instance of my class. I’ve reentered the code in a third party code checker and it runs without error. I’ve seen some forum posts on similar issues saying that indentation was the cause of the error, my indents are not showing up correctly in this post but I’ve rewritten them several times and am still left with the same error.
code:
age =
sex =
bmi =
num_children =
smoker =
region =
charges =
#function to load cvs data
def load_data(lst, csv_file, column_name):
# open the csv file
with open(csv_file) as csv_info:
# read data from that file
csv_dict = csv.DictReader(csv_info)
# loops through each row of the csv
for row in csv_dict:
lst.append(row[column_name])
# return our list
return lst
load_data(age, ‘insurance.csv’, ‘age’)
load_data(sex, ‘insurance.csv’, ‘sex’)
load_data(bmi, ‘insurance.csv’, ‘bmi’)
load_data(num_children, ‘insurance.csv’, ‘children’)
load_data(smoker, ‘insurance.csv’, ‘smoker’)
load_data(region, ‘insurance.csv’, ‘region’)
load_data(charges, ‘insurance.csv’, ‘charges’)
class PatientInfo:
# init function takes in all the lists we’ve built
def init(self, patient_age, patient_sex, patient_bmi, patient_num_chidren, patient_smoker, patient_region, patient_charges):
self.patient_age = patient_age
self.patient_sex = patient_sex
self.patient_bmi = patient_bmi
self.patient_children = patient_num_children
self.patient_smoker = patient_smoker
self.patient_region = patient_region
self.patient_charges = patient_charges
patient_info = PatientInfo(age, sex, bmi, num_children, smoker, region, charges)
Error:
NameError Traceback (most recent call last)
in
----> 1 patient_info = PatientInfo(age, sex, bmi, num_children, smoker, region, charges)
in init(self, patient_age, patient_sex, patient_bmi, patient_num_chidren, patient_smoker, patient_region, patient_charges)
5 self.patient_sex = patient_sex
6 self.patient_bmi = patient_bmi
----> 7 self.patient_children = patient_num_children
8 self.patient_smoker = patient_smoker
9 self.patient_region = patient_region
NameError: name ‘patient_num_children’ is not defined