Build a Medical Data Validator

Hello everyone,

i’m currently working on the “Medical Data Validator” project in the python course(step 18)

here’s a piece of my code:

if set(dictionary.keys()) != key_set:

            print(f"Invalid format: {dictionary} at position {index} has missing and/or invalid keys.")

            is_invalid = True

and i keep getting this message for this step:

You should print Invalid format: <dictionary> at position <index> has missing and/or invalid keys. (where <dictionary> and <index> should be replaced by the dictionary and index at the current iteration) inside your new if statement.

Does anyone know whats wrong here? How do i fix this?

Hi. Can you please link to the url of the step you are on so we can better help you

Remember indentation is important here. You are working inside the for loop

Thanks! my code is already inside the for loop and properly indented but the test still somehow doesn’t pass, i don’t get it

please post your updated code, so we can check what else is not working then

Here it is:

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

key_set = set(

    \['patient_id', 'age', 'gender', 'diagnosis', 'medications', 'last_visit_id'\]

)

for index, dictionary in enumerate(data):

    if not isinstance(dictionary, dict):

        print(f'Invalid format: expected a dictionary at position {index}.')

        is_invalid = True



    if set(dictionary.keys()) != key_set:

        print(f"Invalid format: {dictionary} at position {index} has missing and/or invalid keys.")

        is_invalid = True

    return False

print('Valid format.')

return True

validate(medical_records)

i ‘ve never checked it all tbh because it is the last part that doesn’t pass

I can’t debug that code, the characters are weirdly escaped, please make sure to share your code in a single code block. You can create code blocks with the </> button or using the backticks markdown syntax

When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

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
    key_set = set(
        ['patient_id', 'age', 'gender', 'diagnosis', 'medications', 'last_visit_id']
    )

    for index, dictionary in enumerate(data):
        if not isinstance(dictionary, dict):
            print(f'Invalid format: expected a dictionary at position {index}.')
            is_invalid = True

        if set(dictionary.keys()) != key_set:
            print(f"Invalid format: {dictionary} at position {index} has missing and/or invalid keys.")
            is_invalid = True
        return False
    print('Valid format.')
    return True



validate(medical_records)

you have changed code you are not asked to change.

the requirement is to add the new code after the first if statement

you changed the if statement that was already present after the loop, which should not be touched, you will need to reset the step

1 Like

That worked! Thank you so much :folded_hands:

1 Like