0
我創建了一個基本上是愛好書的類。這本書可以通過兩種方法進行訪問,enter(n,h)
,它需要一個名字並且不斷添加愛好到這個名字(一個名字可以有多個愛好)。另一種方法返回一組特定名稱的業餘愛好。我的愛好書是存儲我插入一個名字的每一個愛好。有人可以幫我修理它嗎?如何將一組多個值添加到一個鍵?
class Hobby:
def __init__(self):
self.dic={}
self.hby=set()
def enter(self,n,h):
if n not in self.dic.items():
self.dic[n]=self.hby
for k in self.dic.items():
self.hby.add(h)
def lookup(self,n):
return self.dic[n]
我試圖運行下列情形
d = Hobby(); d.enter('Roj', 'soccer'); d.lookup('Roj')
{'soccer'}
d.enter('Max', 'reading'); d.lookup('Max')
{'reading', 'soccer'} #should return just reading
d.enter('Roj', 'music'); d.lookup('Roj')
{'reading', 'soccer','music'} #should return soccer and music