Python Simple Banking System
The following example demonstrates the operation of a Python banking system for learning purposes:
Example
means = [0, 0, 0]
loan = 0
rate = 0
pay = 0
investment = 0
annual_rate = 0
# Calculate the expected return of a fixed investment
# The formula for calculating the return of a fixed investment is: M=a(1+x)[-1+(1+x)^n]/x;
# Where M represents the expected return, a represents the amount invested per period, x represents the rate of return, and n represents the number of investment periods.
# Assuming the user invests 300 yuan per month, which is 3600 yuan per year, with an annual return rate of 15%,
# And the investment period is 35 years, the return can be calculated as 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
print("Expected income from fixed 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("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("Withdrawal amount cannot be less than 0!")
elif drawing > means[0]:
print("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) * rate:
print("You will never be able to repay this loan!!!")
else:
if years == 0:
count = 0
while loan > 0:
loan -= pay
loan *= (1 + rate)
count += 1
print("You will repay the loan in %d years." % count)
else:
for _ in range(years):
loan -= pay
if loan == 0:
break
else:
loan *= (1 + rate)
print("Your current debt 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)
capital = means[0] + income - debt
print("Your total assets for year %i are: %.3f" % (years, capital))
def init():
print()
print('''Available services:
1. Check 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 you wish to process: ")
# Check 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 fixed investment
elif choice == "4":
investment = float(input("Please enter the monthly fixed investment amount: "))
annual_rate = float(input("Please enter the annual yield: "))
years = int(input("Please enter the investment term (years): "))
if investment <= 0 or annual_rate <= 0 or years <= 0:
print("Incorrect input data")
else:
money = fixed_investment(investment, annual_rate, years)
print("Final return: %.2f yuan" % money)
# Calculate loan
elif choice == "5":
loan = float(input("Please enter the current loan amount: "))
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("Incorrect input data")
else:
loans(loan, rate, pay, 0)
elif choice == "6":
years = int(input("How many years into the future would you like to check your financial status? "))
future(years)
# Exit
elif choice == "q":
print("Welcome back next time! Goodbye!")
break
else:
print("Incorrect command input, please try again\n")
if __name__ == '__main__':
main()