Question 27
What is the output of the following code?
Hint
The Item class contains 3 class members.
Form their respective values with the parameters passed into the constructor.
Then, follow along with the called methods in subsequent method calls.
Solution
labubu is initialized as an object of class Item with the following class members:
self.name:name = 'labubu'self.price:price = 300self.qty:qty = 3self.revenue:0
Let's now work through the rest of the subsequent statements.
labubu.discount(100):- Since
100is less thanlabubu.price = 300,labubu.priceis now300 - 100 = 200.
- Since
labubu.buy():- Since
labubu.qty = 3 > 0, decrementlabubu.qtyto2. Then, incrementlabubu.revenueby the value oflabubu.price, i.e.,0 + 200 = 200.
- Since
labubu.discount(250):- Since
250is more thanlabubu.price = 200,labubu.priceremains unchanged at200.
- Since
labubu.buy():- Since
labubu.qty = 2 > 0, decrementlabubu.qtyto1. Then, incrementlabubu.revenueby the value oflabubu.price, i.e.,200 + 200 = 400.
- Since
labubu.discount(50):- Since
50is less thanlabubu.price = 200,labubu.priceis now200 - 50 = 150.
- Since
labubu.buy():- Since
labubu.qty = 1 > 0, decrementlabubu.qtyto0. Then, incrementlabubu.revenueby the value oflabubu.price, i.e.,400 + 150 = 550.
- Since
labubu.buy():- Since
labubu.qty = 0, do nothing.labubu.revenueremains at550.
- Since
print(labubu.revenue) now prints 550.