Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON: please add 2 more classes and 2 more methods to each of those classes th

ID: 3577190 • Letter: P

Question

PYTHON: please add 2 more classes and 2 more methods to each of those classes that relates to the game. As long as it relates to the game the 2 classses and 4 methods can be anything.

class MathZombie:
    left = 0
    right = 0

    def __init__(self, a, b):
        self.left = a
        self.right = b

    def add(self):
        return self.left + self.right

    def multiply(self):
        return self.left * self.right

    def isEven(self):
        if (self.left % 2 == 0 and self.right % 2 == 0):
            return True
        else:
            return False

    def drawWinningPattern(self):
        for i in range(4):
            for j in range(1, i):
                print "###########",
            print "^^^^^^"

    def drawLoserPattern(self):
        for i in range(2):
            for j in range(1,i):
                print "~~~~~~~~~",
            print "{}{}{}{}"

#class for the strory to continue after you exit the barn; this is the user class
class User:
    username = ''
    def __init__(self, name):
        username = name

    def encounterBigZombie(self):
        print "'you exit the barn and walk 1 mile out when suddenly...'"
        print "BIG MATH ZOMBIE!!"
        print "'you can't run, the big math zombie is blocking your path, You MUST FIGHT!'"
        print "You would need to any two integers. Enter first integer now: "
        left = input()
        right = input("Enter second integer: ")
        bigZombie = MathZombie(left, right)
        result = input("In order to kill zombie give correct answer. What is: "+str(left)+" * "+str(right)+": ")
        if (result == bigZombie.multiply()):
            print "Good job. You won!"
            bigZombie.drawWinningPattern()
        else:
            print "Wrong!!!! The correct answer is: " + str(bigZombie.multiply())
            print "You lost."
            bigZombie.drawLoserPattern()

    def choiceOnePath(self):
        print "You and your friend, Steven, are camping at a deserted barn. What would you like to do?"
        print "Enter '1' to talk your friend, Steven?"
        print "Enter '2' to exit the barn?"
        choiceOnePath = input("Enter your choice: ")
        while (choiceOnePath != 1 and choiceOnePath != 2 and choiceOnePath != 420):
            choiceOnePath = input("That's Not an Option! Press either '1' or '2'.")

        if (choiceOnePath == 1):
            print " !!!----------------------Chapter One: Escape----------------------!!!"
            print " You: Hey Steven, how's your broken leg healing up?"
            print "Steven: Not so good. I need you to go and get me pain medication from the CVC 3 miles down, can you do that for me?"
            print "You: Sure man, I'll try to be back before dark."
            print "'you exit the barn with a pocket knife, a gun with 3 bullets, and your watch"
        elif (choiceOnePath == 2):
            print " !!!----------------------Chapter One: Escape----------------------!!!"
            print " You: Steven Im going out for a bit, do you need anything?"
            print "Steven: Ya, if you can get me some meds to ease up the pain for my broken leg..."
            print "You: Sure man, I'll try to be back before dark with those meds, see you soon."
            print "'you exit the barn with a pocket knife, a gun with 3 bullets, and your watch"
        elif (choiceOnePath == 420):
            print "Cheat code activated. You won the game my friend!!"
            fakeZombie = MathZombie(0, 0)
            fakeZombie.drawWinningPattern()

        return choiceOnePath

    def writeToFile(self, name):
        f = open("sasha.txt", "w")
        print "Writing to the file: "+ name
        f.write(name)
        f.close()

    def readFromFile(self):
        f = open("sasha.txt", "r")
        print "Data read from file:"
        f.readline()
        f.close()

def main():
    name = raw_input("You are one of the few to survive a zombie apocolyps, what is your name?")
    user = User(name)
    user.writeToFile(name)
    user.readFromFile()

    choiceOnePath = user.choiceOnePath()
    if (choiceOnePath == 1):
        print "***********************************************"
        print "Bad choice. Something bad is about to happen."
        user.encounterBigZombie()
    elif (choiceOnePath == 2):
        print "***********************************************"
        print "Good choice. All you have to do now is to enter to even numbers. If you make a mistake you will lose"
        a = input("Enter first number: ")
        b = input("Enter second number: ")
        smallZombie = MathZombie(a, b)
        if (smallZombie.isEven()):
            print "Correct answer. You won!"
            smallZombie.drawWinningPattern()
        else:
            print "You are so dumb! You lost. :("
            smallZombie.drawLoserPattern()
main()

Explanation / Answer

To solve this issue, you break up pieces of functionality into their own classes and encapsulate all the logic. Then when you want to work on the class, you don't need to think about what else is going on in the code. You can just focus on that small piece of code. This is invaluable for working efficiently, however it's hard to appreciate without working on applications that are huge.

Of course there are countless other benefits to breaking your code into smaller pieces: the code is more maintainable, more testable, more resusable, etc., but to me the largest benefit is that it makes massive programs manageable by reducing the amount of code you need to think about at one time.


The simplest answer is that if you put everything into one class, you have to worry about everything at once when you're writing new code. This may work for small projects, but for huge applications (we're talking hundreds of thousands of lines), this quickly becomes next to impossible.