Build a Medical Data Validator - Step 44

Tell us what’s happening:

I am stuck on it telling me to print the right thing even though that is what I’m printing.

Your code so far

import re


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 find_invalid_records(
    patient_id, age, gender, diagnosis, medications, last_visit_id
):

    constraints = {
        'patient_id': isinstance(patient_id, str)
        and re.fullmatch('p\d+', patient_id, re.IGNORECASE),
        'age': isinstance(age, int) and age >= 18,
        'gender': isinstance(gender, str) and gender.lower() in ('male', 'female'),
        'diagnosis': isinstance(diagnosis, str) or diagnosis is None,
        'medications': isinstance(medications, list)
        and all([isinstance(i, str) for i in medications]),
        'last_visit_id': isinstance(last_visit_id, str)
        and re.fullmatch('v\d+', last_visit_id, re.IGNORECASE)
    }

    return [key for key, value in constraints.items() if not value]


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
            continue

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


# User Editable Region

        invalid_records = find_invalid_records(**dictionary)
        for index, key, val in invalid_records:
            print(f"Unexpected format '{key}: {val}' at position {index}.")
            is_invalid = True

# User Editable Region

    if is_invalid:
        return False
    print('Valid format.')
    return True

validate(medical_records)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Medical Data Validator - Step 44

What type of structure is invalid_records? What does it contain? Test by changing medical_records to have invalid keys or values.

invalid_records contains the dictionary of find_invalid_records. Whenever I change medical_records to invalid key/values, I get an error.

are you sure about that? review where it comes from, test with print, test your assumptions

Can you provide more information about what you did? What error did you get?

I changed paitient_id in medical_records to an integer, and I got a ValueError.

Yes. Good. And it’s referencing line 85 saying there are too many values to unpack.

Leave that in place and add this line under your assignment to invalid_records:
print(type(invalid_records), invalid_records)

What do you see?

Like this?

If this is how you wanted me to put it then I still get a value error on line 85 and 94.

No,

Add the print() line after you assign a value to the invalid_records variable

The goal is to see what you’ve assigned to invalid_records and what data type it is, so you can print it right after you’ve assigned it.

Does that make sense?

The line you are adding is this:

After this:

Please try to share code and not screenshots as wel

Ok, I added the print line in the correct space.

The output is:

<class ‘list’> [‘paitient_id’]

Followed by a value error.

So now can you say what type of data structure invalid_records is and what it contains?

So, when you loop over it, what value will you get? And how does that value relate to dictionary?

I believe the data structure is a List, but I do not know how to answer the rest of the questions.

<class ‘list’> [‘paitient_id’]

Yes. It’s a list. First we printed the type which gave <class ‘list’>. Then we printed the list itself, which gave [‘paitient_id’] and ‘paitient_id’ is what part of a key:value pair in dictionary?

Make a few more entries in medical_records invalid so you have more examples of what the invalid_records list contains.

Also note that a list does not contain key: value pairs.

‘paitient_id’ is a key that holds a value.

patient_id is first of all a string, so you need to treat it as such

then it’s also important to know that it is a key in a dictionary

How will you use that key to get the value?

Review your code because you are using nested loops so there are a few variables in play.

Can you locate the outer loop?

The outer loop should be line 71.

Yes. And what are you getting from that loop that you can use in your code for this step?

Am I creating the dictionary?