Example of a Python Bank System
This is an example of a Python bank system for learning purposes. It simulates operations such as savings, withdrawals, loans, fixed investments, and financial forecasting.
Code Example:
means = [0, 0, 0] # Stores assets: [savings, investment, liabilities] loan = 0 rate = 0 pay = 0 investment = 0 annual_rate = 0 # Calculate expected returns on fixed investments # Formula: M = a(1+x)[-1 + (1+x)^n] / x def fixed_investment(inv, a_rate, y): global means inv = 12 * inv # Monthly investment a_rate = a_rate / 100 # Convert percentage to decimal if a_rate == 0: expected = 0 else: expected = inv * (1 + a_rate) * (pow((1 + a_rate), y) - 1) / a_rate print("Expected returns from fixed investment: %.2f" % expected) means[1] = expected return expected # Check account balance def balance(): total = sum(means) print("Your total assets are: %.2f" % total) print("Detailed assets breakdown:\n") print("Savings: %.2f" % means[0]) print("Investments: %.2f" % means[1]) print("Liabilities: %.2f" % means[2]) # Deposit money def saving(amount): global means if amount < 0: print("Deposit amount cannot be less than 0!") else: means[0] += amount print("Deposited: %.2f" % amount) print("Current balance: %.2f" % means[0]) # Withdraw money 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 balance!") else: means[0] -= drawing print("Withdrawn: %.2f" % drawing) print("Current balance: %.2f" % means[0]) # Calculate loan repayment schedule def loans(loan, rate, pay, years): global means if pay < (loan - pay) * rate: print("You'll never finish paying it off!") else: if years == 0: count = 0 while loan > 0: loan -= pay loan *= (1 + rate) count += 1 print("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 debt is: %.2f" % loan) return loan # Forecast future financial situation 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 in year %i will be: %.3f" % (years, capital)) # Display available services def init(): print('''Available services: 1. Check assets 2. Deposit money 3. Withdraw money 4. Calculate fixed investment 5. Calculate loan 6. Forecast future assets q. Exit''') # Main program logic def main(): init() while True: choice = input("Enter the code for the service you need: ") if choice == "1": balance() elif choice == "2": inc = float(input("Enter deposit amount: ")) saving(inc) elif choice == "3": dec = float(input("Enter withdrawal amount: ")) draw_money(dec) elif choice == "4": investment = float(input("Enter monthly investment amount: ")) annual_rate = float(input("Enter annual return rate (%): ")) years = int(input("Enter investment duration (years): ")) if investment <= 0 or annual_rate <= 0 or years <= 0: print("Invalid input data.") else: money = fixed_investment(investment, annual_rate, years) print("Final returns: %.2f" % money) elif choice == "5": loan = float(input("Enter current loan amount: ")) rate = float(input("Enter annual interest rate (%): ")) pay = float(input("Enter annual repayment amount: ")) if loan <= 0 or rate <= 0 or pay <= 0: print("Invalid input data.") else: loans(loan, rate, pay, 0) elif choice == "6": years = int(input("How many years into the future do you want to forecast? ")) future(years) elif choice == "q": print("Thank you! Goodbye!") break else: print("Invalid input, please try again.") if __name__ == '__main__': main()
Output Example:
Available services: 1. Check assets 2. Deposit money 3. Withdraw money 4. Calculate fixed investment 5. Calculate loan 6. Forecast future assets q. Exit
This example demonstrates the basic operations of a bank system in Python, including handling savings, investments, loans, and forecasting future financial conditions.