Last active
July 22, 2017 14:49
-
-
Save mudspringhiker/ef7eb2cba133af516d99db7c6087b8bc to your computer and use it in GitHub Desktop.
Musicians and Band Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Musician(object): | |
def __init__(self, sounds): | |
self.sounds = sounds | |
def solo(self, length): | |
for i in range(length): | |
print(self.sounds[i % len(self.sounds)], end=" ") | |
print() | |
class Bassist(Musician): # The Musician class is the parent of the Bassist class | |
def __init__(self): | |
# Call the __init__ method of the parent class | |
super().__init__(["Twang", "Thrumb", "Bling"]) | |
def __repr__(self): | |
return "Bassist" | |
class Guitarist(Musician): | |
def __init__(self): | |
# Call the __init__ method of the parent class | |
super().__init__(["Boink", "Bow", "Boom"]) | |
def __repr__(self): | |
return "Guitarist" | |
def tune(self): | |
print("Be with you in a moment") | |
print("Twoning, sproing, splang") | |
class Drummer(Musician): | |
def __init__(self): | |
super().__init__(["Bam", "Baboomboom", "Crack"]) | |
def __repr__(self): | |
return "Drummer" | |
def solo(self, length): | |
for i in range(length): | |
print(self.sounds[i % len(self.sounds)], end=" ") | |
print("Ka-Booom!!!") | |
print() | |
class Band: | |
def __init__(self): | |
self.members = [] | |
def describe(self): | |
for member in self.members: | |
if member != self.members[-1]: | |
print(member, end=", ") | |
else: | |
print(member) | |
def hire(self, musician): | |
self.members.append(musician) | |
print("{} is hired in the band!".format(musician)) | |
return self.members | |
def fire(self, musician): | |
if musician in self.members: | |
for i, member in enumerate(self.members): | |
if musician == member: | |
print("{} is fired from the band.".format(musician)) | |
del self.members[i] | |
return self.members | |
else: | |
return "That musician is not a member of the band." | |
def solos(self): | |
for member in self.members: | |
member.solo(4) | |
print("Instantiating musicians...") | |
danny = Drummer() | |
jerry = Guitarist() | |
kramer = Bassist() | |
george = Guitarist() | |
print() | |
print("Instantiating the band, quartet...") | |
quartet = Band() | |
print() | |
print("Hire band members") | |
quartet.hire(jerry) | |
quartet.hire(kramer) | |
quartet.hire(danny) | |
quartet.hire(george) | |
print() | |
print("Description of the band") | |
quartet.describe() | |
print() | |
print("Fire a musician") | |
quartet.fire(jerry) | |
print() | |
print("Describe band after firing a musician") | |
quartet.describe() | |
print() | |
print("danny's solo") | |
danny.solo(4) | |
print() | |
quartet.solos() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment