将字典项作为字符串返回时出错? (python)
Error in returning dictionary items as string? (python)
我有一个 class 有一个空字典属性:
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]}"
现在当我这样做时:
m1= Library({})
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
代码运行正常,项目被添加到 m1
字典中。
但是当我输入这个时:
print(m1.song2String("Saturday Night's Alright for Fighting"))
我收到此错误消息:
return f"str{x[0]} str{title} (str{x[1]}), str{x[2]}"
TypeError: 'NoneType' object is not subscriptable
我的语法错误是什么?
您在 library
键上进行迭代:for i in self.library.keys()
,但是由于有 none,什么也没做,使其工作的最小更改是
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
break
else:
self.library[title] = [artist, genre, playCount]
但最好只是测试标题是否存在,然后做正确的事情。
def addSong(self, title, artist, genre, playCount):
if title in self.library:
self.library[title][2] += playCount
else:
self.library[title] = [artist, genre, playCount]
您当前的代码returnNone,这是导致您出错的原因。我不知道你为什么要循环插入你的歌曲,直接用字典:
class Library:
def __init__(self,library):
self.library={}
def addSong(self,title,artist,genre,playCount):
if title in self.library: ## changed code here and below
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]}"
输出:
m1= Library({})
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
print(m1.library)
# {"Saturday Night's Alright for Fighting": ['Elton John', 'Rock', 22]}
print(m1.song2String("Saturday Night's Alright for Fighting"))
# Elton John Saturday Night's Alright for Fighting (Rock), 22
I Suppose this is what you expected. In here In addsong function,
first I check is any keys available the dictionary. If it is true,
iterate and do what you want. otherwise, add the first dictionary item
to the dictionary. Inside the songs2string method, first you want to
identify how a dictionary works. In this case, key is a string and the
value is list in the dictionary. So then You can return what you want
using a dictionary.
class Library:
def __init__(self,library=dict()):
self.library=library
def addSong(self,title,artist,genre,playCount):
if len(self.library.keys())>0:
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]
# print(self.library)
def song2String(self,title):
x=self.library
print(x)
return x[title][0], x[title][1], x[title][2]
m1= Library()
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
print(m1.song2String("Saturday Night's Alright for Fighting"))
我有一个 class 有一个空字典属性:
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]}"
现在当我这样做时:
m1= Library({})
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
代码运行正常,项目被添加到 m1
字典中。
但是当我输入这个时:
print(m1.song2String("Saturday Night's Alright for Fighting"))
我收到此错误消息:
return f"str{x[0]} str{title} (str{x[1]}), str{x[2]}"
TypeError: 'NoneType' object is not subscriptable
我的语法错误是什么?
您在 library
键上进行迭代:for i in self.library.keys()
,但是由于有 none,什么也没做,使其工作的最小更改是
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
break
else:
self.library[title] = [artist, genre, playCount]
但最好只是测试标题是否存在,然后做正确的事情。
def addSong(self, title, artist, genre, playCount):
if title in self.library:
self.library[title][2] += playCount
else:
self.library[title] = [artist, genre, playCount]
您当前的代码returnNone,这是导致您出错的原因。我不知道你为什么要循环插入你的歌曲,直接用字典:
class Library:
def __init__(self,library):
self.library={}
def addSong(self,title,artist,genre,playCount):
if title in self.library: ## changed code here and below
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]}"
输出:
m1= Library({})
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
print(m1.library)
# {"Saturday Night's Alright for Fighting": ['Elton John', 'Rock', 22]}
print(m1.song2String("Saturday Night's Alright for Fighting"))
# Elton John Saturday Night's Alright for Fighting (Rock), 22
I Suppose this is what you expected. In here In addsong function, first I check is any keys available the dictionary. If it is true, iterate and do what you want. otherwise, add the first dictionary item to the dictionary. Inside the songs2string method, first you want to identify how a dictionary works. In this case, key is a string and the value is list in the dictionary. So then You can return what you want using a dictionary.
class Library:
def __init__(self,library=dict()):
self.library=library
def addSong(self,title,artist,genre,playCount):
if len(self.library.keys())>0:
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]
# print(self.library)
def song2String(self,title):
x=self.library
print(x)
return x[title][0], x[title][1], x[title][2]
m1= Library()
m1.addSong("Saturday Night's Alright for Fighting","Elton John","Rock",22)
print(m1.song2String("Saturday Night's Alright for Fighting"))