user10116070
user10116070

Reputation:

How to correctly initialize a class

For my python assignment, I have to create classes and initialize them. I created them correctly but the automatic grader says they are not correctly initialized.

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.

What I have so far:

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) fails to correctly initialize artist. and the constructor for both default parameters of Artist and Artwork fail.

What am I missing?

Upvotes: 2

Views: 21748

Answers (3)

Python4Me
Python4Me

Reputation: 9

This is what I got.

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

Upvotes: 0

CircadianR
CircadianR

Reputation: 1

'''

class Artist:
    def __init__(self, name=str(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 < 0:
           print (f'Artist: {self.name}, born {self.birth_year}')
        else:
           print (f'Artist: {self.name} ({self.birth_year}-{self.death_year})')
 
class Artwork:
    def __init__(self, title=str(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 (f'Title: {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()

'''

Upvotes: 0

Dropkicklippy
Dropkicklippy

Reputation: 31

Looks like the exercise underwent multiple variable updates and they forgot to update the template for a lot of the init values. Anyways this should work fine.

main.py

from Artist import Artist
from Artwork import Artwork

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

Artist.py

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('Artist: {}, born {}'.format(self.name, self.birth_year))
    else:
        print('Artist: {} ({}-{})'.format(self.name, self.birth_year, self.death_year))

Artwork.py

from 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_artist

def print_info(self):
    self.artist.print_info()
    print('Title: %s, %d' % (self.title, self.year_created))

Upvotes: 1

Related Questions