I have this dictionary (name and grade):
d1 = {'a': 1, 'b': 2, 'c': 3}
and I have to print it like this:
|a | 1 | C |
|b | 2 | B |
|c | 3 | A |
I created a new dictionary to find out the letter grading based on these conditions:
d2 = {}
d2 = d1
for (key, i) in d2.items():
if i = 1:
d2[key] = 'A'
elif i = 2:
d2[key] = 'B'
elif i = 3:
d2[key] = 'C'
When trying to print it using the following code:
sorted_d = sorted(d)
format_str = '{:10s} | {:10f} | {:>7.2s} |'
for name in sorted_d:
print(format_str.format(name, d[name]))
It prints:
a | 1 |
b | 2 |
c | 3 |
How can I add the letter grade?
See Question&Answers more detail:os