-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.py
79 lines (54 loc) · 1.29 KB
/
bank.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
69
70
71
72
73
74
75
76
77
78
79
## Global
# ## Creating a balance
# balance = 0
# def main():
# print("Balance:", balance)
# if __name__ == "__main__":
# main()
# ## Code outputs a 'UnBoundLocalError'
# balance = 0
# def main():
# print("Balance:", balance)
# deposit(100)
# withdraw(50)
# print("Balance:", balance)
# def deposit(n):
# balance += n
# def withdraw(n):
# balance -= n
# if __name__ == "__main__":
# main()
# ## How to approach error from code above, by making varibale into a global variable
# balance = 0
# def main():
# print("Balance:", balance)
# deposit(100)
# withdraw(50)
# print("Balance:", balance)
# def deposit(n):
# global balance
# balance += n
# def withdraw(n):
# global balance
# balance -= n
# if __name__ == "__main__":
# main()
## Integrating Object Oriented Programming
class Account:
def __init__(self):
self._balance = 0
@property
def balance(self):
return self._balance
def deposit(self, n):
self._balance += n
def withdraw(self, n):
self._balance -= n
def main():
account = Account()
print("Balance:", account.balance)
account.deposit(100)
account.withdraw(50)
print("Balance:", account.balance)
if __name__ == "__main__":
main()