In your example, classes (dictionary) are updated every time with a new key pair, values.
>>> grades = { 'k':'x'}
>>> grades
{'k': 'x'}
>>> grades = { 'newinput':'newval'}
>>> grades
{'newinput': 'newval'}
>>>
What you should have done was update a pair of keys, values ββfor the same dict:
>>> grades = {}
>>> grades['k'] = 'x'
>>> grades['newinput'] = 'newval'
>>> grades
{'k': 'x', 'newinput': 'newval'}
>>>
Try :
>>> grades = {}
>>> while True:
... k = raw_input('Please enter the module ID: ')
... val = raw_input('Please enter the grade for the module: ')
... grades[k] = val
...
Please enter the module ID: x
Please enter the grade for the module: 222
Please enter the module ID: y
Please enter the grade for the module: 234
Please enter the module ID: z
Please enter the grade for the module: 456
Please enter the module ID:
Please enter the grade for the module:
Please enter the module ID: Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>>
>>> grades
{'y': '234', 'x': '222', 'z': '456', '': ''}
>>>
source
share