Digi_B
Digi_B

Reputation: 191

Changing values in a dictionary (python)

I have a dictionary of names of students and their grades. I need to calculate the percentage of these grades and it's weight ( (grade/point)*100 ) and then update their values with the new grades.

I have the following code but the problem is that when I print the dictionary it gives me the old numbers. How can I change the values of the dictionary into the ones after I made the calculations?

maxx = 50
perecent = 100

grades = {'a':36, 'b':25, 'c':43}

for u in grades.values():
    w = ((u/maxx)*(perecent))
    print(w)

print(grades)

The output of this is:

72
86
50
{'a': 36, 'c': 43, 'b': 25}

while I need:

72
86
50
{'a': 72, 'c': 86, 'b': 50}

Upvotes: 0

Views: 5054

Answers (1)

rchang
rchang

Reputation: 5236

for (key, u) in grades.items():
  w = ((u/maxx)*(perecent))
  print(w)
  grades[key] = w
print(grades)

Using key to look up a dictionary entry should effect a change in the value.

Upvotes: 1

Related Questions