I am really stuck with this exercise. How can I with a for loop add the same keys with a new values to a different dictionary in Python?
Goal is to loops through student grades and replace them with "Excellent", "Bad", etc.
This is as far as I could get:
student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
student_grades = {}
for key, value in student_scores.items():
print(key, value)
print(student_grades)
Much appreciated in advance!
>Solution :
You can add values to a dictionary by simply assigning the value to the dictionary indexed by the key. For example, student_grades["Harry"] = "Excellent"
You can do the same in your loop:
for key, value in student_scores.items():
student_grades[key] = "Excellent"
if course, you’ll not assign the same value (grade) for all keys. Use an if statement or any other conditional logic to select which grade to assign to each student. For example:
for key, value in student_scores.items():
if (value > 90)
student_grades[key] = "Excellent"
else
student_grades[key] = "Okey-ish"