In: Computer Science
9.9 LAB: Artwork label (classes/constructors)
Define the Artist class with a constructor to initialize an artist's information and a print_info() method. The constructor should by default initialize the artist's name to "None" and the years of birth and death to 0. print_info() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise.
Define the Artwork class with a constructor to initialize an artwork's information and a print_info() method. The constructor should by default initialize the title to "None", the year created to 0, and the artist to use the Artist default constructor parameter values.
class Artist:
def __init__(self,name, birth_year, death_year):
self.name = 'None'
self.birth_year = 0.0
self.death_year = 0.0
def print_info(self):
if death_year == -1:
print('Artist:', name,'born' ,birth_year)
else:
print('Artist:', name,'(',birth_year,'-',death_year,')')
class Artwork:
def __init__(self,title,year_created,artist):
self.title = 'none'
self.year_created = 0.0
self.artist = Artist(name,birth_year,death_year)
def print_info(self):
print('Title:',end= ' ')
print(title,end= ' ')
print(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()
Whats wrong with my code? It keeps saying Title and Artist arn't defined. And it won't print class artist.
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! Here is how you need to use the default values in both classes. =========================================================================== class Artist(): def __init__(self, name=None, birth_year=0, death_year=0): self.name = name self.birth_year = birth_year self.death_year = death_year def print_info(self): if self.death_year == -1: print('{}, born {}'.format(self.name, self.birth_year)) else: print('{} ({}-{})'.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): print('Title: {}'.format(self.title)) print('Artist: ', end='') self.artist.print_info() def main(): user_artist_name = input('Enter artist name: ') user_birth_year = int(input('Enter birth year: ')) user_death_year = int(input('Enter death year: ')) user_title = input('Enter master piece title: ') user_year_created = int(input('Enter year created: ')) 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() if __name__ == "__main__": main()
=====================================================================
===========================================================================
Thanks, let me know for any questions or in case you encounter any issue.
Please do give a thumbs up from your end : )