在microsoftvisualstudio中,我得到错误“Artist object has no attribute”object。我做错什么了?下面是我的python代码:
class Artist:
def __init__(self, name):
self.name = name
self.albums = []
def add_album(self, album):
self.albums.append(album)
def printLists(self):
print('Songs by {}'.format(self.name))
for alb in self.name:
alb.printFunct('Songs by {}'.format(self.name))
class Album:
#define what is in the album
def __init__(self, albumTitle, artist):
self.songs = []
self.albumTitle = albumTitle
artist.add_album(self)
self.cls = albumTitle
artist.add_album(self)
def addSongs(self, songTitle):
self.songs.append(songTitle)
def printFunct(self):
for song in self.songs:
print('{}({})'.format(song.nameSong, self.albumTitle))
class Song:
def __init__(self, title, album):
self.title = title
self.album = album
album.addSongs(self)
class Playlist:
def __init__(self, name):
self.name = name
self.songs = []
def addSongs(self, song):
self.songs.append(song)
def printSongs(self):
print(self.name)
for song in self.songs:
print('{}'.format(song.title))
hueyLewis = Artist( "Huey Lewis and the News" )
hallAndOats = Artist( "Hall and Oates" )
toto = Artist( "Toto" )
bigBamBoom = Album( "Big Bam Boom", hallAndOats )
sports = Album( "Sports", hueyLewis )
theSeventhOne = Album( "The Seventh One", toto )
four = Album( "IV", toto )
s1 = Song( "If This is it", sports )
s2 = Song( "Bad is Bad", sports )
s3 = Song( "Out of Touch", bigBamBoom )
s4 = Song( "Did it in a minute ", bigBamBoom )
s5 = Song( "Pamela", theSeventhOne )
s6 = Song( "Africa", four )
myAwesomePlaylist = Playlist( "My Awesome Playlist " )
myAwesomePlaylist.addSongs( s1 )
myAwesomePlaylist.addSongs( s2 )
myAwesomePlaylist.addSongs( s3 )
myAwesomePlaylist.addSongs( s4 )
myAwesomePlaylist.addSongs( s5 )
myAwesomePlaylist.addSongs( s6 )
hallAndOats.print('Songs by {}'.format(self.name))
hueyLewis.print('Songs by {}'.format(self.name))
toto.print('Songs by {}'.format(self.name))
myAwesomePlaylist.print()
输出应该打印出艺术家和艺术家的歌曲等
您没有为任何类定义print方法。因此,这些类创建的对象没有print属性。我相信这是你想要的:
编辑
在
^{pr2}$Artist.printLists()
中有两个不同的错误。该方法应该如下所示:你在以艺术家的名义而不是艺术家制作的专辑中迭代单个角色。另外,您错误地调用了
Album.printFunct()
。正如您定义的那样,它没有任何显式参数,但是您是用'Songs by {}'.format(self.name)
作为显式参数来调用它的。在编辑2
您的代码中还有一个错误,但这次是
Album.printFunct()
。该方法应该如下所示:您使用的是
song.nameSong
,它在代码中没有其他地方出现(至少是作为Song
的实例变量)。在编辑3
您在
Album.__init__
中有另一个错误。您将每个专辑添加到每个艺术家两次。从Album
构造函数中删除一个artist.add_album(self)
以修复此问题。在我相信错误是从这里来的:
你应该用
.Artist类的打印列表
.printFunct用于类“Album”和
。为类“Playlist”打印歌曲
只能使用在类中定义的函数。希望有帮助。:)
您的错误告诉您:艺术家对象没有
print
属性。他们有printLists
。只需将最后几行更改为使用printLists
,而不是print
。但是,播放列表应该是printSongs
。在相关问题 更多 >
编程相关推荐