I have a class that has an empty dictionary attribute:
class Library:
def __init__(self,library):
self.library={}
def addSong(self,title,artist,genre,playCount):
for i in self.library.keys():
if i == title:
x=self.library.get(title)
x[2]= x[2]+1
else:
self.library[title]=[artist,genre,playCount]
def song2String(self,title):
x=self.library.get(title)
return f"{x[0]} {title} ({x[1]}), {x[2]}"
Now when I do this:
m1= Library({})
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
The code runs properly and items are added to the m1 dictionary.
But when I type this:
print(m1.song2String("Saturday Night's Alright for Fighting"))
I get this error message:
return f"str{x[0]} str{title} (str{x[1]}), str{x[2]}"
TypeError: 'NoneType' object is not subscriptable
What is my syntax error?
self.library.get(title)is returningNone, which would either indicate thattitleis not a key inself.library, or that the value atself.library[title]isNone.