2

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?

  • 2
    Your code looks perfectly fine! Get in touch with the prof. Maybe they meant _"`print_info()` should display Artist Name, born XXXX if the year of death is **0**"_ – Pranav Hosangadi Oct 14 '20 at 17:20
  • I tried `a = Artist('Pablo Picasso', 1881, 1973)` then `a.print_info()` and it worked perfectly. Why do you think it doesn't work? – Barmar Oct 14 '20 at 17:25
  • 1
    Don't use the default value `user_artist = Artist()`. See https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument – Barmar Oct 14 '20 at 17:27
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Oct 14 '20 at 17:37
  • Don't expect us to guess what your auto-grader wants; give us a test case that fails, along with the results. If you have trouble matching your assignment statement to a black-box grader that we can't even access, then you should take up the problem with your instructor or classmates. – Prune Oct 14 '20 at 17:37
  • @Prune the code above is the test case that failed. https://imgur.com/Vc4CFTX this is the result of me entering that code in. It automatically enters in : Pablo Picasso 1881 1973 Three Musicians 1921 –  Oct 14 '20 at 18:01
  • Could they want you to initialize the names to `None` instead of `"None"`? IDK why they'd say `"None"` then, but the answer to your question is _ask your prof. or TA._ We don't know what they intended for their unit tests to check, and it seems like your code fulfils all requirements specified in the problem description. – Pranav Hosangadi Oct 14 '20 at 18:11
  • 1
    Please refer to the posting guidelines and complete the posting: your post must be self-contained. Off-site links are not acceptable. You have several people trying to *guess* as to your problem requirements. That may be appropriate for a help site, but does not fit Stack Overflow. – Prune Oct 14 '20 at 18:47

3 Answers3

1

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

'''

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

'''

0

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()
Python4Me
  • 11
  • 3