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())
Jug 类中的溢出方法有效,但是,当我从 JugPuzzle 类中的计算方法调用溢出方法时,它说 JugPuzzle 没有当前属性。我是不是叫错了还是什么?