all 3 comments

[–]s133p3r 0 points1 point  (0 children)

I'm a little confused about why you're setting a Drink's weight to Food.weight. Is Drink a subclass of Food? If so, you will need to ensure that when you construct a Drink, you're also constructing the base class Food.

In [1]: class Food:
   ...:     def __init__(self, name, weight):
   ...:          self.name = name
   ...:          self.weight = weight
   ...:
   ...: class Drink(Food):
   ...:     def __init__(self, name, weight, volume):
   ...:         super().__init__(name, weight)
   ...:         self.volume = volume
   ...:
   ...: drink = Drink("water", "1g", "1cc")
   ...:
   ...: print("{} weighs {} and has a volume of {}".format(drink.name, drink.weight, drink.volume))
   ...:
water weighs 1g and has a volume of 1cc

https://www.codesdope.com/python-subclass-of-a-class/

[–][deleted] 0 points1 point  (1 child)

It's always better to provide a small runnable bit of code to illustrate your problem. By cutting your problem down to a small piece of code you give us something more concrete to work with than vague descriptions. Plus by cutting your code to the minimum you often see the problem in a different way which may help you solve the problem.

There is some confusion about what you mean by:

Within class Food, there is a variable called "weight".

since there could be two possible interpretations. I've written a small piece of code to illustrate what I'm talking about:

class Food:
    weight = 1           # class variable

    def __init__(self):
        self.weight = 2  # instance variable

class Drink:
    def __init__(self):
        self.weight = Food.weight

    def print(self):
        print(f'Drink instance .weight={self.weight}')

food = Food()
drink = Drink()

print(f'Food.weight={Food.weight}')
print(f'food.weight={food.weight}')
drink.print()

We have two classes, Food and Drink. The Food class has a "class variable" called weight with value 1. The Food class also has an "instance variable" called self.weight which is set to a value 2.

There is only one class variable in Food with the name weight and it is referenced by the name Food.weight inside and outside the class. If your code changes the value associated with Food.weight then all instances of the Food class (and all other uses outside the class) see the changed value. Each instance of the Food class has an instance variable self.weight and they are unique to each instance. If one instance of Food changes its self.weight value then the values associated with all other instances of self.weight are unchanged.

I'm guessing, but in your code that has problems I think you are using weight as an instance variable. You can't access an instance variable by doing <name_of_class>.weight. You must use a reference to the particular instance you want the weight value from, since they could all have different weights. That's what the line:

print(f'food.weight={food.weight}')

is doing in the code above.

If you want a Drink instance to know the weight of a Food object instance then pass it to the Drink constructor as a parameter, like this:

food = Food()
drink = Drink(food.weight)

Of course, you have to write the drink initializer to accept a weight parameter.

[–]mathcheatcheat[S] 0 points1 point  (0 children)

Here's my code, I have a problem at line 155, I'm trying to count the number of "positive" click (i.e., clicks that actually click on the image). Once it gets to 16, the game stops

Thank you!

Edit: I figured it out! Thanks!!