如何正确初始化一个类

对于我的 python 作业,我必须创建类并初始化它们。我正确创建了它们,但自动评分器说它们未正确初始化。

使用初始化艺术家信息的构造函数和 print_info() 方法定义 Artist 类。默认情况下,构造函数应将艺术家的姓名初始化为“None”,并将出生年份和死亡年份初始化为 0。 print_info() 应显示艺术家姓名,如果死亡年份为 -1,则应显示出生 XXXX,否则应显示艺术家姓名 (XXXX-YYYY) 。

使用构造函数定义 Artwork 类以初始化艺术品的信息和 print_info() 方法。默认情况下,构造函数应将标题初始化为“None”,创建年份为 0,并且艺术家应使用 Artist 默认构造函数参数值。

到目前为止我所拥有的:

class Artist:

   def __init__(self, user_artist_name="None", user_birth_year=0, user_death_year=0):

       self.name = user_artist_name

       self.birth = user_birth_year

       self.death = user_death_year


   def print_info(self):

       if self.death == -1:

           print("Artist: {}, born {}".format(self.name, self.birth))

       else:

           print("Artist: {} ({}-{})".format(self.name, self.birth, self.death))



class Artwork:

   def __init__(self, user_title= "None", year_created=0, user_artist=Artist()):

       self.title = user_title

       self.year = year_created

       self.artist = user_artist


   def print_info(self):

       print("Title: {}, {}".format(self.title, self.year))



if __name__ == "__main__":

   user_artist_name = input()

   user_birth_year = int(input())

   user_death_year = int(input())

   user_title = input()

   user_year_created = int(input())


   user_artist = Artist(user_artist_name, user_birth_year, user_death_year)

   

   user_artist.print_info()


   new_artwork = Artwork(user_title, user_year_created, user_artist)


   new_artwork.print_info()

Artist('Pablo Picasso', 1881, 1973) 未能正确初始化艺术家。并且 Artist 和 Artwork 的默认参数的构造函数都失败。


我缺少什么?


慕森王
浏览 79回答 3
3回答

慕神8447489

看起来该练习经历了多个变量更新,并且他们忘记更新许多初始值的模板。无论如何,这应该可以正常工作。main.pyfrom Artist import Artistfrom Artwork import Artworkif __name__ == "__main__":    user_artist_name = input()    user_birth_year = int(input())    user_death_year = int(input())    user_title = input()    user_year_created = int(input())    user_artist = Artist(user_artist_name, user_birth_year, user_death_year)    new_artwork = Artwork(user_title, user_year_created, user_artist)    new_artwork.print_info()Artist.pyclass Artist:def __init__(self, name="None", birth_year=0, death_year=0):    self.name = name    self.birth_year = birth_year    self.death_year = death_yeardef print_info(self):    if self.death_year == -1:        print('Artist: {}, born {}'.format(self.name, self.birth_year))    else:        print('Artist: {} ({}-{})'.format(self.name, self.birth_year, self.death_year))Artwork.pyfrom Artist import Artist class Artwork:def __init__(self, user_title="None", year_created=0, user_artist=Artist()):    self.title = user_title    self.year_created = year_created    self.artist = user_artistdef print_info(self):    self.artist.print_info()    print('Title: %s, %d' % (self.title, self.year_created))

慕容708150

这就是我得到的。class Artist:    def __init__(self, name = 'None', birth_year = 0.0, death_year = 0.0):        self.name = name        self.birth_year = birth_year        self.death_year = death_year    def print_info(self):        if self.death_year == -1:            print('Artist: {}, born {}'.format(self.name, self.birth_year))        else:            print('Artist: {} ({}-{})'.format(self.name, self.birth_year, self.death_year))  class Artwork:    def __init__(self, title = 'None', year_created = 0, artist = Artist()):        self.title = title        self.year_created = year_created        self.artist = artist    def print_info(self):        self.artist.print_info()        print('Title: {}, {}'.format(self.title, self.year_created))if __name__ == "__main__":    user_artist_name = input()    user_birth_year = int(input())    user_death_year = int(input())    user_title = input()    user_year_created = int(input())    user_artist = Artist(user_artist_name, user_birth_year, user_death_year)    new_artwork = Artwork(user_title, user_year_created, user_artist)      new_artwork.print_info()

翻阅古今

'''class Artist:&nbsp; &nbsp; def __init__(self, name=str(None), birth_year=0, death_year=0):&nbsp; &nbsp; &nbsp; &nbsp; self.name = name&nbsp; &nbsp; &nbsp; &nbsp; self.birth_year = birth_year&nbsp; &nbsp; &nbsp; &nbsp; self.death_year = death_year&nbsp; &nbsp; def print_info(self):&nbsp; &nbsp; &nbsp; &nbsp; if self.death_year < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print (f'Artist: {self.name}, born {self.birth_year}')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print (f'Artist: {self.name} ({self.birth_year}-{self.death_year})')&nbsp;class Artwork:&nbsp; &nbsp; def __init__(self, title=str(None), year_created=0, artist=Artist()):&nbsp; &nbsp; &nbsp; &nbsp; self.title = title&nbsp; &nbsp; &nbsp; &nbsp; self.year_created = year_created&nbsp; &nbsp; &nbsp; &nbsp; self.artist = artist&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def print_info(self):&nbsp; &nbsp; &nbsp; &nbsp; self.artist.print_info()&nbsp; &nbsp; &nbsp; &nbsp; print (f'Title: {self.title}, {self.year_created}')if __name__ == "__main__":&nbsp; &nbsp; user_artist_name = input()&nbsp; &nbsp; user_birth_year = int(input())&nbsp; &nbsp; user_death_year = int(input())&nbsp; &nbsp; user_title = input()&nbsp; &nbsp; user_year_created = int(input())&nbsp; &nbsp; user_artist = Artist(user_artist_name, user_birth_year, user_death_year)&nbsp; &nbsp; new_artwork = Artwork(user_title, user_year_created, user_artist)&nbsp; &nbsp; new_artwork.print_info()'''
打开App,查看更多内容
随时随地看视频慕课网APP