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 f9ddff6

Browse files
More infor added
1 parent b314573 commit f9ddff6

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

‎Classes/state.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,39 @@ def withdraw(amount):
2323
sanjay = deposit(100)
2424
print(sanjay)
2525

26-
# The above example is good enough only if we want to have just a single account.
26+
# The above example is good enough only if we want to have just a single account. Did you understand what I said?
27+
2728

2829
# ******Things start getting complicated if want to model multiple accounts.******
2930

31+
# We can solve the problem by making the state local, probably by using a dictionary to store the state.
32+
33+
def make_account():
34+
return {'balance': 0}
35+
36+
def putDeposit(account, amount):
37+
account['balance'] += amount
38+
return account['balance']
39+
40+
def withdrawMoney(account, amount):
41+
account['balance'] -= amount
42+
return account['balance']
43+
44+
def checkBalance(account):
45+
return account['balance']
46+
47+
48+
sanjay = make_account()
49+
50+
ram = make_account()
51+
52+
# by default, when someone creates an account, balance will show 0.
53+
putDeposit(sanjay, 5000)
54+
55+
# what is sanjay's balance amount now?
56+
print("Sanjay has " + str(checkBalance(sanjay)) + " in his account!")
57+
58+
print("Sanjay has withdrawn 2500 rs from the account, therefore the remaining balance is " + str(withdrawMoney(sanjay, 2500)))
59+
60+
61+

0 commit comments

Comments
(0)

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