Build a Medical Data Validator - Step 44

Tell us what’s happening:

I keep raising an indentation error with the 6th line from bottom up. Since the validator is about complete, I have tried repeatedly to finish. Please advise me.

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

        invalid_records = find_invalid_records(**dictionary)

# User Editable Region

for pos,key in enumerate(invalid_records):
    val=dictionary[key] 
    print(f"Unexpected format '{key}: {val}' at position {pos}")     

# User Editable Region

if is_invalid:
return True 
pass     
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/143.0.0.0 Safari/537.36 Edg/143.0.0.0

Challenge Information:

Build a Medical Data Validator - Step 44

Why is all the indentation removed from the last lines?

Does that look correct to you?

This lab does have a slightly complex structure of a function with nested loops but look over the whole program from top to bottom. Try to make sense of where the function and loops begin and end.

Should you be all the way to the left right now?

What variables are you working with? Where were they created and in what scope or context do they exist?

If you’re unable to fix the indentation of the bottom, you’ll need to reset the step.

Thankyou. The console is clear. I was able to fix the indentation issue. Still no pass. Shows problem with the for loop.

for pos,key in enumerate(invalid_records):

    val=dictionary\[key\] 

print(f"Unexpected format '{key}: {val}' at position {pos}")

what problem does it show with the for loop?

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 (').

Message when I check:

You should have a for loop that iterates over invalid_records.

and is your loop iterating over that? exactly that?

I have copy/pasted my for loop above as it is now and I don’t know how to edit it. I need help.

yeah, and I am asking you, is your loop iterating exactly on invalid_records? or there is something that is not only and exactly invalid_records, like, you know, enumerate

I have

for index in (invalid_records): before print now and there is no alarm in the console. but no pass, still asking for iteration.

please post your code, just describing like that I don’t know what you have written exactly

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 (').

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

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

print('Valid format.') 
return True

validate(medical_records)

please use backticks to format your code to receive the best help possible

I don’t know if you have everything that should be inside validate actually unindented or it’s a thing from the misformatting

Am I allowed to continue with the lessons that follow and come back to this particular exercise with better preparation?

You can do what you want but I don’t see how far you’ll get if you can’t

  • indent your code correctly
  • paste your code into the forum formatted correctly

You must learn to do both of these things.

Should I use the backticks while coding or posting into the forum.

I know where the back tick is located on the keyboard. It has raised error messages as I use it during forming a new block. I expect it may be because I do not allot it a separate line.

Please advise.

the backtisk are needed to format the code for the forum

here the instructions again

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 (').

You are correct. Put the backticks on their own line.

Read the instructions posted above. Do you have any questions about it?

A block of code with 3 backticks on a separate line should look like this.

```
code
code
```

I am going to ask please tell me what is considered a ‘block’ of code.

Code, any code. Code, because you need to keep the format intact and that’s what the backticks will do.

Please test it out and let us know if you have any questions.

A google search said a block of code is under a function or after for and if. I put back ticks at those points and I get error messages.