1

I am stuck in this question (part two). The question is as follows: Create a class called AppleBasket who's constructor accepts two inputs: a string representing a color, and a number representing a quantity of apples. The constructor should initialize two instance variables: apple_color and apple_quantity. Write a class method called increase that increases the quantity by 1 each time it is invoked. You should also write a str method for this class that returns a string of the format: "A basket of [quantity goes here] [color goes here] apples." e.g. "A basket of 4 red apples." or "A basket of 50 blue apples."

My code is:

class AppleBasket():
    def __init__(self, apple_color, apple_quantity):
        self.apple_color = apple_color
        self.apple_quantity = apple_quantity

    def getC(self):
        return self.apple_color

    def getQ(self):
        return self.apple_quantity

    def __str__(self):
        return "A basket of {} {} apples.".format(self.apple_quantity, self.apple_color)

    def increase(self):
        return self.apple_quantity + 1


print(AppleBasket("Red", 4))

However, Quantity still remains at 4 instead of 5.

Can some please advice me what mistake am I doing? Thank you.

Austin
  • 25,759
  • 4
  • 25
  • 48
Bhavin
  • 15
  • 1
  • 6

3 Answers3

3

correct your increase method:

    def increase(self):
        self.apple_quantity += 1
erfan mehraban
  • 483
  • 3
  • 5
  • 13
  • Hi. Thanks, it worked. However, why is that return statement has to be removed. : Like it gives syntax error if I wrote return self.apple_quantity += 1. Bit confused here. – Bhavin Nov 18 '19 at 10:06
  • 1
    You need to think about what is actually happening. `+=` is an addition assignment operator. An alternative way of achieving the same result would be `self.apple_quantity = self.apple_quantity + 1` , which obviously you cannot have as a `return` value. There is more in [this answer](https://stackoverflow.com/questions/38461509/why-cant-return-assignment-statement) – PyPingu Nov 18 '19 at 10:25
  • Well understood. Thanks. – Bhavin Nov 18 '19 at 12:51
  • Why does this reflect "increases the quantity by 1 **each time it is invoked**" ? `def increase(self):` `self.apple_quantity += 1` – Kailey May 03 '21 at 21:50
0
class AppleBasket():


    def ___init__(self, color, quantity):

         self.apple_color = color
         self.apple_quantity = quantity

    def increase(self):
         self.apple_quantity += 1
       

    def __str__(self):
          return "A basket of {} {} apples.".format(self.apple_quantity, self.apple_color)
     
-1

class AppleBasket: def init(self,apple_color,apple_quantity): self.apple_color=apple_color self.apple_quantity=apple_quantity

  def ap(self):
        self.apple_color=apple_color
  
  def apl(self):
        self.apple_quantity=apple_quantity
  

  def __str__(self):
        return "A basket of {} {} apples.".format(self.apple_quantity,self.apple_color)
  
  def increase(self):
        self.apple_quantity += 1

print(AppleBasket("green",4))