Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Create a function get_song_string that returns a string in the following format: "<song name>" - <artist name> (<song year>)
- e.g: "You Belong With Me" - Taylor Swift (2008)
- '''
- class Artist:
- def __init__(self, name, label):
- self.name = name
- self.label = label
- class Song:
- def __init__(self, name, album, year, artist):
- self.name = name
- self.album = album
- self.year = year
- self.artist = artist
- #My method
- def get_song_string(self):
- return "\"" + self.name + "\""+ " - " + self.artist.name + " (" + str(self.year) + ")"
- '''
- #Model answer method - alternative string formatting syntax
- # By using .format with placeholders
- def get_song_string(a_song):
- return '"{0}" - {1} ({2})'.format(a_song.name, a_song.artist.name, a_song.year)
- '''
- new_artist = Artist("Taylor Swift", "Big Machine Records, LLC")
- new_song = Song("You Belong With Me", "Fearless", 2008, new_artist)
- print(get_song_string(new_song))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement