3 回答
TA贡献1780条经验 获得超1个赞
看起来该练习经历了多个变量更新,并且他们忘记更新许多初始值的模板。无论如何,这应该可以正常工作。
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))
TA贡献1831条经验 获得超4个赞
这就是我得到的。
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()
TA贡献1780条经验 获得超5个赞
'''
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()
'''
- 3 回答
- 0 关注
- 131 浏览
添加回答
举报