Build a Medical Data Validator - Step 9

Tell us what’s happening: again this code runs in Spyder6 and shows:

Invalid format: expected a dictionary at position 1
Invalid format: expected a dictionary at position 3
Is data invalid? True

“Should have and if statement in for loop”
def validate_dict_format(data):
is_invalid = False
for index, item in enumerate(data):
if not isinstance(item, dict):
print(f"Invalid format: expected a dictionary at position {index}")
is_invalid = True
return is_invalid

Your code so far

medical_records = [
    {
        'patient_id': 'P1001',
        'age': 34,
        'gender': 'Female',
        'diagnosis': 'Hypertension',
        'medications': ['Lisinopril'],
        'last_visit_id': 'V2301',
    },
    {
        'patient_id': 'p1002',
        'age': 47,
        'gender': 'male',
        'diagnosis': 'Type 2 Diabetes',
        'medications': ['Metformin', 'Insulin'],
        'last_visit_id': 'v2302',
    },
    {
        'patient_id': 'P1003',
        'age': 29,
        'gender': 'female',
        'diagnosis': 'Asthma',
        'medications': ['Albuterol'],
        'last_visit_id': 'v2303',
    },
    {
        'patient_id': 'p1004',
        'age': 56,
        'gender': 'Male',
        'diagnosis': 'Chronic Back Pain',
        'medications': ['Ibuprofen', 'Physical Therapy'],
        'last_visit_id': 'V2304',
    }
]

#def validate(data):
#    is_sequence = isinstance(data, (list, tuple))

#    if not is_sequence:
#       print('Invalid format: expected a list or tuple.')
#        return False
        

# User Editable Region

#    is_invalid = False

def validate_dict_format(data):
    is_invalid = False
    for index, item in enumerate(data):
        if not isinstance(item, dict):
            print(f"Invalid format: expected a dictionary at position {index}")
            is_invalid = True
    return is_invalid

# Example usage
data = [{'a': 1}, 'invalid', {'b': 2}, 3, {'c': 4}]
is_invalid = validate_dict_format(data)
print(f"Is data invalid? {is_invalid}")

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Medical Data Validator - Step 9

where is the loop for index, dictionary in enumerate(data):? you may want to reset the step and try again, changing loop variables will not allow the tests to pass

Is this not it? Or am I looking at another indent issue?

def validate_dict_format(data):
is_invalid = False
for index, item in enumerate(data):
if not isinstance(item, dict):
print(f"Invalid format: expected a dictionary at position {index}")
is_invalid = True
return is_invalid

I do not see index, dictionary as variables of the loop, do you? you changed them, so you need to reset the step

step 8

medical_records = [
{
‘patient_id’: ‘P1001’,
‘age’: 34,
‘gender’: ‘Female’,
‘diagnosis’: ‘Hypertension’,
‘medications’: [‘Lisinopril’],
‘last_visit_id’: ‘V2301’,
},
{
‘patient_id’: ‘p1002’,
‘age’: 47,
‘gender’: ‘male’,
‘diagnosis’: ‘Type 2 Diabetes’,
‘medications’: [‘Metformin’, ‘Insulin’],
‘last_visit_id’: ‘v2302’,
},
{
‘patient_id’: ‘P1003’,
‘age’: 29,
‘gender’: ‘female’,
‘diagnosis’: ‘Asthma’,
‘medications’: [‘Albuterol’],
‘last_visit_id’: ‘v2303’,
},
{
‘patient_id’: ‘p1004’,
‘age’: 56,
‘gender’: ‘Male’,
‘diagnosis’: ‘Chronic Back Pain’,
‘medications’: [‘Ibuprofen’, ‘Physical Therapy’],
‘last_visit_id’: ‘V2304’,
}
]

def validate(data):
is_sequence = isinstance(data, (list, tuple))

if not is_sequence:
    print('Invalid format: expected a list or tuple.')
    return False
    
is_invalid = False

for index, dictionary in enumerate(data):
print(index, dictionary)
pass

this is there Error

how to define the ‘data’

Your code raised an error before any tests could run. Please fix it and try again.

hi @nazriramzanvi

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.