In this part of the project, we will create differentiated pet classes that demonstrate the use of both inheritance and polymorphism in OOP to create different kinds of animals.
import random
class ePet:
def __init__(self, strN):
self.strName = strN
self.intHunger = 5
self.intBoredom = 5
print(f"You adopted an ePet named {self.strName}.")
def __str__(self):
return f"{self.strName} ".ljust(15)+"Boredom:".ljust(10, ".") + f"{self.intBoredom}".rjust(5, ".")+ " Hunger:".ljust(10, ".") + f"{self.intHunger}".rjust(5, ".")
def feedPet(self):
print(f"You fed {self.strName}.")
self.intHunger += 3
def playPet(self):
print(f"You played with {self.strName}.")
self.intBoredom += 3
def takeTurn(self):
self.intBoredom -= random.randint(0,2)
self.intHunger -= random.randint(0,2)
print(f"Time passes... {self.strName} is getting more bored and hungry.")
def listPets(liPets):
intIndex = 1
for pet in liPets:
print(f"{intIndex} - {pet.strName}")
intIndex += 1
print("ePet Shop".center(60))
liPets = []
while True:
print("""
1. Feed Pet
2. Play with Pet
3. Adopt a New Pet
""")
strChoice = input("What do you want to do?")
if strChoice == "1":
listPets(liPets)
intChoice = input("Which pet?")
if intChoice.isnumeric():
intChoice = int(intChoice) - 1
liPets[intChoice].feedPet()
else:
print("Invalid Input")
elif strChoice == "2":
listPets(liPets)
intChoice = input("Which pet?")
if intChoice.isnumeric():
intChoice = int(intChoice) - 1
liPets[intChoice].playPet()
else:
print("Invalid Input")
elif strChoice == "3":
strName = input("What do you want to name your pet?")
liPets.append(ePet(strName))
else:
print("Invalid Choice. You lose a turn!")
for pet in liPets:
pet.takeTurn()
for pet in liPets:
if pet.intHunger < 1 or pet.intBoredom < 1:
liPets.remove(pet)
print(f"{pet.strName} has run away!")
print("Here are your pets:")
for pet in liPets:
print(pet)
import random
class ePet:
def __init__(self, strN):
self.strName = strN
self.intHunger = 5
self.intBoredom = 5
print(f"You adopted an ePet named {self.strName}.")
def __str__(self):
return f"{self.strName} ".ljust(15)+"Boredom:".ljust(10, ".") + f"{self.intBoredom}".rjust(5, ".")+ " Hunger:".ljust(10, ".") + f"{self.intHunger}".rjust(5, ".")
def feedPet(self):
print(f"You fed {self.strName}.")
self.intHunger += 3
def playPet(self):
print(f"You played with {self.strName}.")
self.intBoredom += 3
def takeTurn(self):
self.intBoredom -= random.randint(0,2)
self.intHunger -= random.randint(0,2)
print(f"Time passes... {self.strName} is getting more bored and hungry.")
class ePuppy(ePet):
def __init__(self, strN):
super().__init__(strN)
def __str__(self):
return f"{self.strName} (Puppy) ".ljust(20)+"Boredom:".ljust(10, ".") + f"{self.intBoredom}".rjust(5, ".")+ " Hunger:".ljust(10, ".") + f"{self.intHunger}".rjust(5, ".")
def playPet(self):
print(f"You had a game of fetch with {self.strName}.")
self.intBoredom += 4
class eFish(ePet):
def __init__(self, strN):
super().__init__(strN)
def __str__(self):
return f"{self.strName} (Fish) ".ljust(20)+"Boredom:".ljust(10, ".") + f"{self.intBoredom}".rjust(5, ".")+ " Hunger:".ljust(10, ".") + f"{self.intHunger}".rjust(5, ".")
def feedPet(self):
print(f"You sprinkle some food into {self.strName}'s tank.")
self.intHunger += 4
def listPets(liPets):
intIndex = 1
for pet in liPets:
print(f"{intIndex} - {pet.strName}")
intIndex += 1
print("ePet Shop".center(60))
liPets = []
while True:
print("""
1. Feed Pet
2. Play with Pet
3. Adopt a New Pet
""")
strChoice = input("What do you want to do?")
if strChoice == "1":
listPets(liPets)
intChoice = input("Which pet?")
if intChoice.isnumeric():
intChoice = int(intChoice) - 1
liPets[intChoice].feedPet()
else:
print("Invalid Input")
elif strChoice == "2":
listPets(liPets)
intChoice = input("Which pet?")
if intChoice.isnumeric():
intChoice = int(intChoice) - 1
liPets[intChoice].playPet()
else:
print("Invalid Input")
elif strChoice == "3":
print("""
Types of Pets:
1. Puppy
2. Fish
""")
strType = input("Type the number of the type of pet you want to adopt: ")
strName = input("What do you want to name your pet?")
if strType == "1":
liPets.append(ePuppy(strName))
elif strType == "2":
liPets.append(eFish(strName))
else:
print("That's not one of the options.")
else:
print("Invalid Choice. You lose a turn!")
for pet in liPets:
pet.takeTurn()
for pet in liPets:
if pet.intHunger < 1 or pet.intBoredom < 1:
liPets.remove(pet)
print(f"{pet.strName} has run away!")
print("Here are your pets:")
for pet in liPets:
print(pet)