Python Simple Banking System
The following example demonstrates the operations of a Python banking system:
Example
means
=
[
0
,
0
,
0
]
loan
=
0
rate
=
0
pay
=
0
investment
=
0
annual_rate
=
0
Calculate the expected return of a recurring investment
The formula for recurring investment returns is: M=a(1+x)[-1+(1+x)^n]/x;
where M is the expected return, a is the amount invested each period, x is the rate of return, and n is the number of periods.
Suppose the user invests 300 yuan per month, that is 3600 yuan a year, with an annual return of 15%,
over a term of 35 years; the return is 3600(1+15%)[-1+(1+15%)^35]/15% = 3648044 yuan.
def
fixed_investment
(
inv
,
a_rate
,
y
)
:
global
means
inv
=
12
- inv
a_rate
=
a_rate /
100
if
a_rate
==
0
:
expected
=
0
else
:
expected
=
inv *
(
1
- a_rate
)
(
pow
(
(
1
- a_rate
)
,
y
)
1
)
/ a_rate
(
"Expected income from the recurring investment: %.2f"
% expected
)
means
[
1
]
=
expected
return
expected
def
balance
(
)
:
total
=
0
for
i
in
means:
total +
=
i
(
"Your total assets are: %.2f"
% total
)
(
"Your asset details are:\n"
)
(
"Deposits: %.2f"
% means
[
0
]
)
(
"Investments: %.2f"
% means
[
1
]
)
(
"Liabilities: %.2f"
% means
[
2
]
)
def
saving
(
amount
)
:
global
means
if
amount
<
0
:
(
"The deposit amount cannot be less than 0!"
)
else
:
means
[
0
]
=
amount
(
"Deposited: %.2f yuan"
% amount
)
(
"Current balance: %.2f yuan"
% means
[
0
]
)
def
draw_money
(
drawing
)
:
global
means
if
drawing
<
0
:
(
"The withdrawal amount cannot be less than 0!"
)
elif
drawing
means
[
0
]
:
(
"The withdrawal amount cannot exceed the balance!"
)
else
:
means
[
0
]
=
drawing
(
"Withdrawn: %.2f yuan"
% drawing
)
(
"Current balance: %.2f yuan"
% means
[
0
]
)
def
loans
(
loan
,
rate
,
pay
,
years
)
:
global
means
if
pay
<
(
loan - pay
)
- rate:
(
"You will never finish paying it off!!!"
)
else
:
if
years
==
0
:
count
=
0
while
loan
0
:
loan -
=
pay
loan *
=
(
1
- rate
)
count +
=
1
(
"The loan will be fully repaid in %d years."
% count
)
else
:
for
_
in
range
(
years
)
:
loan -
=
pay
if
loan
==
0
:
break
else
:
loan *
=
(
1
- rate
)
(
"Your current liability is: %.2f"
% loan
)
means[2] = loan
return
loan
Future financial status
def
future
(
years
)
:
income
=
fixed_investment
(
investment
,
annual_rate
,
years
)
debt
=
loans
(
loan
,
rate
,
pay
,
years
)
captial
=
means
[
0
]
- income - debt
(
"Your total assets in year %i are: %.3f"
%
(
years
,
captial
)
)
def
init
(
)
:
(
)
(
'''The available services are:
1. Query assets
2. Deposit
3. Withdraw
4. Calculate compound interest
5. Calculate loan
6. Calculate future assets
q. Exit'''
)
def
main
(
)
:
init
(
)
while
True
:
choice
=
input
(
"Please enter the service code: "
)
Query balance
if
choice
==
"1"
:
balance
(
)
Deposit
elif
choice
==
"2"
:
inc
=
float
(
input
(
"Please enter the deposit amount: "
)
)
saving
(
inc
)
Withdraw
elif
choice
==
"3"
:
dec
=
float
(
input
(
"Please enter the withdrawal amount: "
)
)
draw_money
(
dec
)
Calculate recurring investment
elif
choice
==
"4"
:
investment
=
float
(
input
(
"Please enter the monthly investment amount: "
)
)
annual_rate
=
float
(
input
(
"Please enter the annual return rate: "
)
)
years
=
int
(
input
(
"Please enter the investment term (years): "
)
)
if
investment
<=
0
or
annual_rate
<=
0
or
years
<=
0
:
(
"Invalid input"
)
else
:
money
=
fixed_investment
(
investment
,
annual_rate
,
years
)
(
"Final result: %.2f yuan"
% money
)
Calculate loan
elif
choice
==
"5"
:
loan
=
float
(
input
(
"Please enter the current loan: "
)
)
rate
=
float
(
input
(
"Please enter the annual interest rate: "
)
)
pay
=
float
(
input
(
"Please enter the annual repayment: "
)
)
if
loan
<=
0
or
rate
<=
0
or
pay
<=
0
:
(
"Invalid input"
)
else
:
loans
(
loan
,
rate
,
pay
,
0
)
elif
choice
==
"6"
:
years
=
int
(
input
(
"How many years ahead do you want to check the financial status? "
)
)
future
(
years
)
Exit
elif
choice
==
"q"
:
(
"Thanks for visiting! Goodbye!"
)
break
else
:
(
"Invalid command, please try again\n"
)
if
name
==
'main'
:
main
(
)
Python3 Examples