Python Simple Banking System
The following examples are for learning the operations of a Python banking system:
Examples
means = [0, 0, 0]
loan = 0
rate = 0
pay = 0
investment = 0
annual_rate = 0
# Calculate expected return of regular investment
# The formula for calculating regular investment return is: M=a(1+x)[-1+(1+x)^n]/x;
# Where M represents the expected return, a represents the regular investment amount per period, x represents the return rate, and n represents the number of investment periods.
# Assume the user invests 300 yuan per month, which is 3600 yuan per year, with an annual return rate of 15%,
# With an investment period of 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 regular 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("Wealth management: %.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 pay it off!!!")
else:
if years == 0:
count = 0
while loan > 0:
loan -= pay
loan *= (1 + rate)
count += 1
print("The loan will be paid off in %d years." % count)
else:
for _ in range(years):
loan -= pay
if loan == 0:
break
else:
loan *= (1 + rate)
print("Your current liabilities are: %.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
print("Your total assets in year %i: %.3f" % (years, captial))
def init():
print()
print('''The following are the available services:
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 you want to use: ")
# Query balance
if choice == "1":
balance()
# Deposit
elif choice == "2":
inc = float(input("Please enter deposit amount: "))
saving(inc)
# Withdrawal
elif choice == "3":
dec = float(input("Please enter withdrawal amount: "))
draw_money(dec)
# Calculate regular investment
elif choice == "4":
investment = float(input("Please enter the monthly regular investment amount: "))
annual_rate = float(input("Please enter the annual return rate: "))
years = int(input("Please enter the investment period (years): "))
if investment <= 0 or annual_rate <= 0 or years <= 0:
print("The input data is incorrect")
else:
money = fixed_investment(investment, annual_rate, years)
print("Final gain: %.2f yuan" % money)
# Calculate loan
elif choice == "5":
loan = float(input("Please enter current loan: "))
rate = float(input("Please enter annual interest rate: "))
pay = float(input("Please enter annual repayment: "))
if loan <= 0 or rate <= 0 or pay <= 0:
print("The input data is incorrect")
else:
loans(loan, rate, pay, 0)
elif choice == "6":
years = int(input("How many years later would you like to check the financial status? "))
future(years)
# Exit
elif choice == "q":
print("Welcome back next time! Goodbye!")
break
else:
print("The command you entered is incorrect, please re-enter\n")
if __name__ == '__main__':
main()
loan = 0
rate = 0
pay = 0
investment = 0
annual_rate = 0
# Calculate expected return of regular investment
# The formula for calculating regular investment return is: M=a(1+x)[-1+(1+x)^n]/x;
# Where M represents the expected return, a represents the regular investment amount per period, x represents the return rate, and n represents the number of investment periods.
# Assume the user invests 300 yuan per month, which is 3600 yuan per year, with an annual return rate of 15%,
# With an investment period of 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 regular 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("Wealth management: %.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 pay it off!!!")
else:
if years == 0:
count = 0
while loan > 0:
loan -= pay
loan *= (1 + rate)
count += 1
print("The loan will be paid off in %d years." % count)
else:
for _ in range(years):
loan -= pay
if loan == 0:
break
else:
loan *= (1 + rate)
print("Your current liabilities are: %.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
print("Your total assets in year %i: %.3f" % (years, captial))
def init():
print()
print('''The following are the available services:
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 you want to use: ")
# Query balance
if choice == "1":
balance()
# Deposit
elif choice == "2":
inc = float(input("Please enter deposit amount: "))
saving(inc)
# Withdrawal
elif choice == "3":
dec = float(input("Please enter withdrawal amount: "))
draw_money(dec)
# Calculate regular investment
elif choice == "4":
investment = float(input("Please enter the monthly regular investment amount: "))
annual_rate = float(input("Please enter the annual return rate: "))
years = int(input("Please enter the investment period (years): "))
if investment <= 0 or annual_rate <= 0 or years <= 0:
print("The input data is incorrect")
else:
money = fixed_investment(investment, annual_rate, years)
print("Final gain: %.2f yuan" % money)
# Calculate loan
elif choice == "5":
loan = float(input("Please enter current loan: "))
rate = float(input("Please enter annual interest rate: "))
pay = float(input("Please enter annual repayment: "))
if loan <= 0 or rate <= 0 or pay <= 0:
print("The input data is incorrect")
else:
loans(loan, rate, pay, 0)
elif choice == "6":
years = int(input("How many years later would you like to check the financial status? "))
future(years)
# Exit
elif choice == "q":
print("Welcome back next time! Goodbye!")
break
else:
print("The command you entered is incorrect, please re-enter\n")
if __name__ == '__main__':
main()
Python3 Examples