AttributeError:对象没有属性打印

时间:2016-03-02 02:34:11

标签: python

在Microsoft Visual Studio中,我收到错误“Artist对象没有属性”对象“。我做错了什么?这是我的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()

输出应该打印出艺术家和艺术家的歌曲等。

5 个答案:

答案 0 :(得分:2)

您没有为任何类定义打印方法。因此,这些类创建的对象将没有print属性。我相信这是你想要的:

hallAndOats.printLists()
hueyLewis.printLists()
toto.printLists()
myAwesomePlaylist.printSongs()

修改

Artist.printLists()中有两个不同的错误。该方法应如下所示:

def printLists(self):
    print('Songs by {}'.format(self.name))
    for alb in self.albums:
        alb.printFunct()

您正在遍历艺术家名称中的各个角色,而不是艺术家制作的专辑。另外,您错误地调用了Album.printFunct()。正如您所定义的那样,它没有任何显式参数,但您使用'Songs by {}'.format(self.name)作为显式参数调用它。

编辑2

您的代码中还有另一个错误,但这次是Album.printFunct()。该方法应如下所示:

def printFunct(self):
    for song in self.songs:
        print('{}({})'.format(song.title, self.albumTitle))

您使用的是song.nameSong,它在您的代码中没有其他地方出现(最不重要的是作为Song的实例变量)。

编辑3

Album.__init__中还有其他错误。您要将每张专辑添加到每位艺术家两次。从artist.add_album(self)构造函数中删除Album之一,以解决此问题。

答案 1 :(得分:1)

您的错误告诉您:艺术家对象没有属性print。他们有printLists。只需将最后一行更改为使用printLists而不是print。但是,播放列表应为printSongs

答案 2 :(得分:0)

我认为错误来自此处:

hallAndOats.print('Songs by {}'.format(self.name))
hueyLewis.print('Songs by {}'.format(self.name))
toto.print('Songs by {}'.format(self.name))
myAwesomePlaylist.print()

你应该使用

。“艺术家”类的.printLists,

类'Album'和的

.printFunct 类别'播放列表'

.printSongs

您只能使用您在课堂上定义的功能。希望能帮助到你。 :)

答案 3 :(得分:0)

您需要在printLists()对象上调用Artist

答案 4 :(得分:0)

我建议你看一下How to create a Minimal, Complete, and Verifiable example。在这种情况下,您可以将代码一直削减到此并仍然遇到同样的问题:

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))

hallAndOats = Artist( "Hall and Oates" )

hallAndOats.print('Songs by {}'.format(self.name))

从那里,应该很清楚,错误的原因是你没有在print个对象上定义一个名为Artist的方法。

(此外,将来,请提供完整的追溯,以便人们可以准确查看异常的内容和位置。)