NameError: U.S Medical Insurance Costs Portfolio Project

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

A NameError suggests you’ve used a name that hasn’t been assigned to at this point. Double check your order and ensure there are no typos. The traceback should point you the line where this occur has occurred (check it carefully).

Please also have a look at the following FAQ which details the best way to set up a question (link your lesson, format code etc.) as it helps immensely for anyone else who tries to help.

3 Likes

There is a typo in your code (line 31): “patient_num_chidren” is different than “patient_num_children” - when you declared the variable in the “init” command, you forgot the “l”.

In addition to what @tgrtim wrote, I recommend that you post your code on the forum in preformatted text mode (Ctrl + Shift + C) since it is easier to read and analyze dozens of lines of code.

1 Like

I can’t believe I missed that… thanks for pointing it out! Sorry for the poor formatting, it’s my first time posting to this forum and I needed to post quickly during my lunch break.