PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Created by Wong Hei Ming
# refactored by Tibor Santa (kwargs version)
class Score():
SUBJECTS = 'english', 'arts', 'math'
def __init__(self, name, **kwargs):
self.name = name
self._scores = {}
for sub, score in kwargs.items():
if sub in Score.SUBJECTS:
self._scores[sub] = score
else:
print('invalid subject:', sub)
def set_score(self, **kwargs):
for sub, score in kwargs.items():
if sub in self._scores:
self._scores[sub] = score
print(f'{sub} changed to {score}')
else:
print(f'missing subject: {sub}')
def __str__(self):
return f'\n{self.name}\n' + '\n'.join(f'{sub}: {score}' for sub, score in self._scores.items()) + '\n'
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run