Day 55 - Creating an ePet Using Object-Oriented Programming (OOP), Part 1

We will begin to explore Object-Oriented Programming using a ePet project to explore basic ideas.

Finished Code

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 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(1,3)
        self.intHunger -= random.randint(1,3)
        print(f"Time passes... {self.strName} is getting more bored and hungry.")
        print("Boredom:".ljust(15,".")+f"{self.intBoredom}".rjust(10,"."))
        print("Hunger:".ljust(15, ".") + f"{self.intHunger}".rjust(10, "."))


print("ePet Shop".center(60))
strName = input("What do you want to name your pet?")

myPet = ePet(strName)

while True:
    myPet.takeTurn()

    if myPet.intHunger < 1 or myPet.intBoredom < 1:
        print(f"{myPet.strName} has run away to find a better owner!")
        break

    print("""
    1. Feed Pet
    2. Play with Pet
    """)

    strChoice = input("What do you want to do?")

    if strChoice == "1":
        myPet.feedPet()
    elif strChoice == "2":
        myPet.playPet()
    else:
        print("Invalid Choice. You lose a turn!")