Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d14b83a

Browse files
authored
Create static2.py
1 parent 9668a1a commit d14b83a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎docs/OOPS/static2.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
However, the solution of hardcoding the value in the attribute is not a good one.
3+
For example, since this is a limited time discount we should be able to programmatically
4+
enable and disable the discount using functions like this:
5+
'''
6+
7+
8+
class Mobile:
9+
def __init__(self, price, brand):
10+
self.price = price
11+
self.brand = brand
12+
self.discount = 0
13+
14+
def purchase(self):
15+
total = self.price - self.price * self.discount / 100
16+
print (self.brand, "mobile with price", self.price, "is available after discount at", total)
17+
18+
def enable_discount(list_of_mobiles):
19+
for mobile in list_of_mobiles:
20+
mobile.discount=50
21+
22+
def disable_discount(list_of_mobiles):
23+
for mobile in list_of_mobiles:
24+
mobile.discount=0
25+
26+
mob1=Mobile(20000, "Apple")
27+
mob2=Mobile(30000, "Apple")
28+
mob3=Mobile(5000, "Samsung")
29+
mob4=Mobile(6000, "Samsung")
30+
31+
list_of_mobiles=[mob1,mob2,mob3,mob4]
32+
33+
mob1.purchase()
34+
35+
enable_discount(list_of_mobiles)
36+
37+
mob2.purchase()
38+
mob3.purchase()
39+
40+
disable_discount(list_of_mobiles)
41+
42+
mob4.purchase()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /