Raz P
Raz P

Reputation: 1

i've been trying to get this code to work for hours and I've tried to fix it in multiple ways and it still doesn't work

i've been trying to get this to work for hours and I've tried to fix it in multiple ways and it still doesn't work.

# this creates my class "bucket"
class bucket:
    def __init__(self, water, capcity):
        self.water = water
        self.capcity = capcity


# here I create my objects
bucket1 = bucket(0, 3)
bucket2 = bucket(0, 5)


# this is the printing code to print how much water there is in every bucket
def pr_v():
    print("Bucket_ONE " + str(bucket1.water) + "L")
    print("Bucket_TWO " + str(bucket2.water) + "L")
    print(" ")


# this is where my problem is
# this scirpt is supposed to trasfer water from the pouring bucket (p_bucket) to the receiving bucket untill the receiving bucket reaches it's
# capacity or the pouring bucket runs out of water
def pour_water(p_bucket, g_bucket):
    while g_bucket.capcity >= g_bucket.water or p_bucket.water != -1:
        if g_bucket.capcity != g_bucket.water or p_bucket.water != -1:

            if g_bucket.capcity < g_bucket.water:
                bucket.water = g_bucket.capcity
                break
            p_bucket.water -= 1
            g_bucket.water += 1
            pr_v()

    if p_bucket.water == -1:
        p_bucket.water = 0


# this fills the bucket to it's capacity 
def fill_water(self):
    self.water = self.capcity


fill_water(bucket2)
pour_water(bucket2, bucket1)
pr_v()

when I execute the code this happens

Bucket_ONE 1L
Bucket_TWO 4L
 
Bucket_ONE 2L
Bucket_TWO 3L

it's supposed to stop here because bucket 1 reaches its capacity

Bucket_ONE 3L
Bucket_TWO 2L
 
Bucket_ONE 4L
Bucket_TWO 1L
 
Bucket_ONE 4L
Bucket_TWO 1L

why does this happen? Im new to coding so I might not know a lot of stuff

Thanks

Upvotes: 0

Views: 58

Answers (3)

Rheo
Rheo

Reputation: 1

remove the last pr_v() it just prints the result again after printing it in your method or remove the pr_v() from your method so that you only see the result

Upvotes: 0

Billy Bonaros
Billy Bonaros

Reputation: 1721

You have to change the following line to add the equals sign:

 if g_bucket.capcity <= g_bucket.water:

Upvotes: 0

Mimi
Mimi

Reputation: 26

Boy did you over complicate this...

def pour_water(p_bucket, g_bucket):
    while not p_bucket.is_full() and not g_bucket.is_empty():
        p_bucket += 1
        g_bucket -= 1
        pr_v()

I am going to leave the implementation of "is_full" and "is_empty" to you. Good luck

Upvotes: 1

Related Questions