-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.py
73 lines (48 loc) · 1.63 KB
/
vault.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## Operator Overloading
# class Vault:
# def __init__(self, galleons=0, sickles=0, knuts=0):
# self.galleons = galleons
# self.sickles = sickles
# self.knuts = knuts
# def __str__(self):
# return f"{self.galleons} Galleons, {self.sickles} Sickles, {self.knuts} Knuts"
# potter = Vault(100, 50, 25)
# print(potter)
# weasley = Vault(25, 50, 100)
# print(weasley)
## Combining vaults
# class Vault:
# def __init__(self, galleons=0, sickles=0, knuts=0):
# self.galleons = galleons
# self.sickles = sickles
# self.knuts = knuts
# def __str__(self):
# return f"{self.galleons} Galleons, {self.sickles} Sickles, {self.knuts} Knuts"
# potter = Vault(100, 50, 25)
# print(potter)
# weasley = Vault(25, 50, 100)
# print(weasley)
# galleons = potter.galleons + weasley.galleons
# sickles = potter.sickles + weasley.sickles
# knuts = potter.knuts + weasley.knuts
# total = Vault(galleons, sickles, knuts)
# print(total)
## Using 'object.__add__(self, other)'
class Vault:
def __init__(self, galleons=0, sickles=0, knuts=0):
self.galleons = galleons
self.sickles = sickles
self.knuts = knuts
def __str__(self):
return f"{self.galleons} Galleons, {self.sickles} Sickles, {self.knuts} Knuts"
def __add__(self, other):
galleons = self.galleons + other.galleons
sickles =self.sickles + other.sickles
knuts = self.knuts + other.knuts
return Vault(galleons, sickles, knuts)
potter = Vault(100, 50, 25)
print(potter)
weasley = Vault(25, 50, 100)
print(weasley)
total = potter + weasley
print(total)