简体   繁体   中英

Iterating through dictionary values in python (.keys() in .values()?)

In a programming course I am now at dictionaries point in python. So I have this task in which I need to add a "fail" command, which basically is printing out students if a grade is less than 4. I have googled it and searched for similar problems in here, but just couldn't find a similar example. Hope you can help me. Also, I have added the code and in "def fail():" you can see what was my idea. But there is an error code with this - ValueError: too many values to unpack (expected 2). PS, I'm new to python.

students = {('Ozols', 'Jānis'): {'Math': '10', 'ProgVal': '5', 'Sports': '5'},
        ('Krumiņa', 'Ilze'): {'Math': '7', 'ProgVal': '3', 'Sports': '6'},
        ('Liepa', 'Peteris'): {'Math': '3', 'ProgVal': '7', 'Sports': '7'},
        ('Lapsa', 'Maris'): {'Math': '10', 'ProgVal': '10', 'Sports': '3'}}

courses = ['Math', 'ProgVal', 'Sports']


def fail():
for lName, fName in students.keys():
    for course, grade in students.values():
        if grade < 4:
            print(fName, lName)


while True:
print()
command = input("command:> ")
command = command.lower()

if command == 'fail':
    fail()
elif command == 'done':
    break
print("DONE")

Try the below

students = {
    ('Ozols', 'Jānis'): {
        'Math': '10',
        'ProgVal': '5',
        'Sports': '5'
    },
    ('Krumiņa', 'Ilze'): {
        'Math': '7',
        'ProgVal': '3',
        'Sports': '6'
    },
    ('Liepa', 'Peteris'): {
        'Math': '3',
        'ProgVal': '7',
        'Sports': '7'
    },
    ('Lapsa', 'Maris'): {
        'Math': '10',
        'ProgVal': '10',
        'Sports': '3'
    }
}

courses = ['Math', 'ProgVal', 'Sports']


def fail():
    for k,v in students.items():
        for course, grade in v.items():
            if int(grade) < 4:
              print(f'{k[0]} {k[1]} failed in {course}')

fail()

output

Krumiņa Ilze failed in ProgVal
Liepa Peteris failed in Math
Lapsa Maris failed in Sports

There're a few issues with your code.

Firstly, you should be using an integer (int) or float to store a student's score. You can fix it by changing them to integers:

students = {('Ozols', 'Jānis'): {'Math': 10, 'ProgVal': 5, 'Sports': 5},
        ('Krumiņa', 'Ilze'): {'Math': 7, 'ProgVal': 3, 'Sports': 6},
        ('Liepa', 'Peteris'): {'Math': 3, 'ProgVal': 7, 'Sports': 7},
        ('Lapsa', 'Maris'): {'Math': 10, 'ProgVal': 10, 'Sports': 3}}

Secondly, you should not be using a loop of students.values() within a loop of students.keys() as it will loop through the entire dictionary again - you want to loop through each subject of the values (which are dictionaries of scores) of each student , which is stored in the list called students .

Try this:

students = {('Ozols', 'Jānis'): {'Math': 10, 'ProgVal': 5, 'Sports': 5},
        ('Krumiņa', 'Ilze'): {'Math': 7, 'ProgVal': 3, 'Sports': 6},
        ('Liepa', 'Peteris'): {'Math': 3, 'ProgVal': 7, 'Sports': 7},
        ('Lapsa', 'Maris'): {'Math': 10, 'ProgVal': 10, 'Sports': 3}}

courses = ['Math', 'ProgVal', 'Sports']


for lName, fName in students.keys():
    studentRecord = students[(lName, fName)]
    for course in studentRecord:
        if studentRecord[course] < 4:
            print(fName, lName, course)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM