0
class Jug:
    def __init__(self, current, highest):
        self.current = current
        self.highest = highest

    def spill(self, jug, jug_1):
        """
        :return: The new values of the jug spilled from and the jug spilled to
        """
        while True:
            self.current[jug] -= 1
            self.current[jug_1] += 1
            if self.current[jug] == 0 or self.current[jug_1] == self.highest[jug_1]:
                break


    def __str__(self):
        """
        :return: The new values of all the jugs
        """
        return "Jug 0: {0}/8 Jug 1: {1}/5 Jug 2: {2}/3".format(self.current[0], self.current[1], self.current[2])


class JugPuzzle:

    def __init__(self):
        self.jugs = [[8, 8], [0, 5], [0, 3]]  # [jug0[0], jug1[0], jug2[0]]
        self.turn = 0

    def calculate(self):
        jug_from = int(input("\nEnter jug to spill from: "))

        if self.jugs[jug_from][0] == 0:
            print("Cannot spill from an empty Jug. Try again")
        else:
            jug_to = int(input("Enter jug to spill to: "))
            if self.jugs[jug_to][0] == self.jugs[jug_to][1]:
                return "Cannot spill into a full Jug"
            else:
                Jug.spill(my_jug, jug_from, jug_to)  # --> Is this call correct?



if __name__ == "__main__":
    jug0 = [8, 8]
    jug1 = [0, 5]
    jug2 = [0, 3]
    jugs = [jug0, jug1, jug2]
    my_jug = Jug([jug0[0], jug1[0], jug2[0]], [jug0[1], jug1[1], jug2[1]])
    (my_jug.spill(0, 1))
    print(my_jug.__str__())

    my_jug = JugPuzzle()
    print(my_jug.calculate())

The spill method in the Jug class works, however, when I call the spill method from the calculate method in the JugPuzzle class, it says JugPuzzle has no attribute current. Am I calling it incorrectly or something?

  • Use `self` not the class name. `self` is the name python provides to reference attributes of a class _inside_ of the same class: `self.split(my_jug, jug_from, jug_to)`. – Christian Dean Jan 16 '17 at 23:12
  • @leaf: I assume you mean `self.spill(my_jug, jug_from, jug_to)`? (Note the `spill` rather than `split`.) – Rory Daulton Jan 16 '17 at 23:19
  • @RoryDaulton Yep, that's what I meant. – Christian Dean Jan 16 '17 at 23:21
  • @leaf If I change `Jug.spill(my_jug, jug_from, jug_to)` to `self.spill(my_jug, jug_from, jug_to)`, instead of the error saying JugPuzzle object has no attribute "current", it'll say JugPuzzle object has no attribute "spill" – Coder Coder Jan 16 '17 at 23:41
  • @CoderCoder My mistake. I thought you were doing something different, and gave you a bad suggestion. Your problem is that you need to create an instance of the Jug class before you can cal any methods: `Jug(my_jug, jug_from, jug_to)`. – Christian Dean Jan 17 '17 at 00:09
  • @leaf So if I do `my_jug = Jug([jug0[0], jug1[0], jug2[0]], [jug0[1], jug1[1], jug2[1]])` and have a different variable name for when I create an instance of my JugPuzzle class, can I call the spill method from my JugPuzzle class by doing `my_jug.spill(jug_from, jug_to)`? – Coder Coder Jan 17 '17 at 00:37
  • @CoderCoder No, you cannot. I suggest taking the [tutorial on Python classes](https://docs.python.org/3/tutorial/classes.html) to get a better idea of how to use them. – Christian Dean Jan 17 '17 at 00:40

0 Answers0