Check if a student has passed a class

Solution 1:

Try this code:

students = {
'Peter': ['Economy', {'PRO100': 'B', 'PRO110': 'C', 'DAT130': 'F'}], 
'James': ['Psychology', {'MAT120': 'C', 'PRO100': 'B', 'DAT120': 'A'}]
}

passed_grades = ['A', 'B', 'C']

def check(student, subject):
    if students[student][1][subject] in passed_grades:
        return True
    return False

Solution 2:

Here is the answer to your code :

students = {
    'Peter': [
        'Economy', {
            'PRO100': 'B', 
            'PRO110': 'C', 
            'DAT130': 'F'
        }
    ], 
    'James': [
        'Psychology', {
            'MAT120': 'C', 
            'PRO100': 'B', 
            'DAT120': 'A'
        }
    ]
}

good_grades = ['A', 'B', 'C']
bad_grades = ['D', 'E', 'F']

def check(student, subject):
    for subjects in students[student]:
        subjectList = subjects
    
    for grade in subjectList:
        if grade == subject:
            if subjectList[grade] in good_grades:
                return True
            if subjectList[grade] in bad_grades:
                return False
            
    return 'Not Found'
    
    
check('Peter', 'PRO100')

Examples :

check('Peter', 'DAT130')
False
check('James', 'DAT120')
True