MusicPlayer exercise in Python

parent 7227bbd4
from player import Player
from track import Track
def run():
# create instance of player
player = Player("myPlayer")
# create instances for the following three tracks
track1 = Track("Incubus", "Drive", "Make Yourself")
track2 = Track("Ritchie Valens", "La Bamba", "La Bamba")
track3 = Track("Red Hot Chilli Peppers", "Californication", "Californication")
# add tracks to player
player.add(track1)
player.add(track2)
player.add(track3)
# play tracks
player.play()
player.next()
player.play()
player.next()
player.play()
player.next()
player.play()
player.previous()
player.play()
# directly select a track
player.selectTrack(1)
player.play()
# print out all tracks
player.printTracksInfo()
if __name__ == "__main__":
run()
# Playing: Drive (Make Yourself) by Incubus
# Playing: La Bamba (La Bamba) by Ritchie Valens
# Playing: Californication (Californication) by Red Hot Chilli Peppers
# Playing: Drive (Make Yourself) by Incubus
# Playing: Californication (Californication) by Red Hot Chilli Peppers
# Playing: La Bamba (La Bamba) by Ritchie Valens
# Track 0: Drive (Make Yourself) by Incubus
# Track 1: La Bamba (La Bamba) by Ritchie Valens
# Track 2: Californication (Californication) by Red Hot Chilli Peppers
\ No newline at end of file
class Player(object):
def __init__(self, tracks):
self.tracks = []
self.actualTrack = 1
def add(self, newTrack):
self.tracks.append(newTrack)
def play(self):
print("Playing: %s (%s) by %s" % (self.tracks[self.actualTrack - 1].title, self.tracks[self.actualTrack -1].album, self.tracks[self.actualTrack - 1].artist))
def next(self):
if self.actualTrack < len(self.tracks):
self.actualTrack = self.actualTrack + 1
else:
self.actualTrack = 1
def previous(self):
if self.actualTrack > 0:
self.actualTrack = self.actualTrack - 1
else:
self.actualTrack = len(self.tracks)
def selectTrack(self, index):
self.actualTrack = index + 1
def printTracksInfo(self):
for x in range(len(self.tracks)):
print("Track " + str(x) + ": " + self.tracks[x].title + " (" + self.tracks[x].album + ") by " + self.tracks[x].artist)
\ No newline at end of file
class Track():
def __init__(self, artist, title, album):
self.artist = artist
self.title = title
self.album = album
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment