[英]AttributeError: object has no attribute print
In Microsoft Visual Studio, I am getting the error "Artist object has no attribute "object". What am I doing wrong? Here is my python code: 在Microsoft Visual Studio中,出现错误“艺术家对象没有属性“对象”。我在做什么错呢?这是我的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()
The output is supposed to print out the artist and the song of the artist etc. 输出应该打印出艺术家和艺术家的歌曲等。
You don't define a print method for any of your classes. 您没有为任何类定义打印方法。 Therefore, the objects created by those classes won't have a print attribute.
因此,这些类创建的对象将没有print属性。 I believe this is what you wanted though:
我相信这是您想要的:
hallAndOats.printLists()
hueyLewis.printLists()
toto.printLists()
myAwesomePlaylist.printSongs()
EDIT 编辑
You have two separate mistakes in Artist.printLists()
. 您在
Artist.printLists()
有两个单独的错误。 That method should look like this: 该方法应如下所示:
def printLists(self):
print('Songs by {}'.format(self.name))
for alb in self.albums:
alb.printFunct()
You were iterating through the individual characters in the name of the artist instead of the albums that the artist produced. 您正在以艺术家的名字而不是艺术家制作的专辑来遍历各个角色。 Also, you were calling
Album.printFunct()
incorrectly. 此外,您错误地调用了
Album.printFunct()
。 As you defined it, it does not have any explicit parameters, yet you were calling it with 'Songs by {}'.format(self.name)
as an explicit argument. 定义时,它没有任何显式参数,但是您使用
'Songs by {}'.format(self.name)
作为显式参数进行调用。
EDIT 2 编辑2
You have yet another error in your code, but this time in Album.printFunct()
. 您的代码中还有另一个错误,但是这次在
Album.printFunct()
。 That method should look like this: 该方法应如下所示:
def printFunct(self):
for song in self.songs:
print('{}({})'.format(song.title, self.albumTitle))
You were using song.nameSong
, which appears nowhere else in your code (least of all as an instance variable of Song
). 您使用的是
song.nameSong
,它在代码中没有其他地方显示(最不作为Song
的实例变量)。
EDIT 3 编辑3
You have another error in Album.__init__
. 您在
Album.__init__
另一个错误。 You are adding every album to each artist twice. 您将每张专辑两次添加到每个艺术家。 Remove one of the
artist.add_album(self)
s from the Album
constructor to fix this. 从
Album
构造函数中删除artist.add_album(self)
以解决此问题。
Your error tells you something: Artist objects don't have the attribute print
. 您的错误告诉您:艺术家对象没有属性
print
。 They have printLists
. 他们有
printLists
。 Just change those last lines to use printLists
instead of print
. 只需将最后
printLists
行更改为使用printLists
而不是print
。 The playlist, however, should be printSongs
. 但是,播放列表应为
printSongs
。
I believe the error is from here: 我相信错误来自这里:
hallAndOats.print('Songs by {}'.format(self.name))
hueyLewis.print('Songs by {}'.format(self.name))
toto.print('Songs by {}'.format(self.name))
myAwesomePlaylist.print()
You should use 你应该用
You can only use the functions you have defined in you class. 您只能使用您在课程中定义的功能。 Hope it helps.
希望能帮助到你。 :)
:)
您需要在Artist
对象上调用printLists()
。
I'd urge you to have a look at How to create a Minimal, Complete, and Verifiable example . 我敦促您看一下如何创建最小,完整和可验证的示例 。 In this case, you could have whittled your code all the way down to this and still had the same problem:
在这种情况下,您可能一直将代码缩水到这一点,但仍然遇到相同的问题:
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))
From there, it should be pretty clear that the reason for the error is that you didn't define a method called print
on Artist
objects. 从那里开始,应该很清楚错误的原因是您没有在
Artist
对象上定义一个称为print
的方法。
(Also, in the future, please provide the full traceback so that people can see exactly what and where the exception is.) (此外,请在将来提供完整的追溯,以便人们可以确切地看到异常的内容和位置。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.