Build a Medical Data Validator - Step 26

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.search(‘p’, patient_id, re.IGNORCASE)
}
return constraints

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

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

validate(medical_records)
print(find_invalid_records(**medical_records[0]))

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Please Help. Where is my error:

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.search(‘p’, patient_id, re.IGNORCASE)
}
return constraints

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

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.search(‘p’, patient_id, re.IGNORCASE)}return constraints

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

if is_invalid:
    return False
print('Valid format.')
return True
validate(medical_records)
print(find_invalid_records(**medical_records[0]))

When I added the re.IGNORCASE as the third argument for the re.search, the program throws an Valid Error message regarding line 42 and line 75. Stating re. has not attribute IGNORCASE.

can you give a link to this step?

wait… are you sure it is IGNORCASE?


for next time please

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.