Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Simple Banking System

Python Simple Banking System

Document Object Reference Manual Python3 Examples

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

    a_rate

=

a_rate /

100

   

if

a_rate

==

0

:

        expected

=

0

   

else

:

        expected

=

inv *

(

1

)

(

pow

(

(

1

)

,

y

)

1

)

/ a_rate

   

print

(

"Expected income from the recurring investment: %.2f"

% expected

)

    means

[

1

]

=

expected

   

return

expected

def

balance

(

)

:

    total

=

0

   

for

i

in

means:

        total +

=

i

   

print

(

"Your total assets are: %.2f"

% total

)

   

print

(

"Your asset details are:\n"

)

   

print

(

"Deposits: %.2f"

% means

[

0

]

)

   

print

(

"Investments: %.2f"

% means

[

1

]

)

   

print

(

"Liabilities: %.2f"

% means

[

2

]

)

def

saving

(

amount

)

:

   

global

means

   

if

amount

<

0

:

       

print

(

"The deposit amount cannot be less than 0!"

)

   

else

:

        means

[

0

]

=

amount

       

print

(

"Deposited: %.2f yuan"

% amount

)

       

print

(

"Current balance: %.2f yuan"

% means

[

0

]

)

def

draw_money

(

drawing

)

:

   

global

means

   

if

drawing

<

0

:

       

print

(

"The withdrawal amount cannot be less than 0!"

)

   

elif

drawing

means

[

0

]

:

       

print

(

"The withdrawal amount cannot exceed the balance!"

)

   

else

:

        means

[

0

]

=

drawing

       

print

(

"Withdrawn: %.2f yuan"

% drawing

)

       

print

(

"Current balance: %.2f yuan"

% means

[

0

]

)

def

loans

(

loan

,

rate

,

pay

,

years

)

:

   

global

means

   

if

pay

<

(

loan - pay

)

       

print

(

"You will never finish paying it off!!!"

)

   

else

:

       

if

years

==

0

:

            count

=

0

           

while

loan

0

:

                loan -

=

pay

                loan *

=

(

1

)

                count +

=

1

           

print

(

"The loan will be fully repaid in %d years."

% count

)

       

else

:

           

for

_

in

range

(

years

)

:

                loan -

=

pay

               

if

loan

==

0

:

                   

break

               

else

:

                    loan *

=

(

1

)

                   

print

(

"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

]

   

print

(

"Your total assets in year %i are: %.3f"

%

(

years

,

captial

)

)

def

init

(

)

:

   

print

(

)

   

print

(

'''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

:

               

print

(

"Invalid input"

)

           

else

:

                money

=

fixed_investment

(

investment

,

annual_rate

,

years

)

           

print

(

"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

:

               

print

(

"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"

:

           

print

(

"Thanks for visiting! Goodbye!"

)

           

break

       

else

:

           

print

(

"Invalid command, please try again\n"

)

if

name

==

'main'

:

    main

(

)

Document Object Reference Manual Python3 Examples