Python 实现简单的银行系统(附带源码)

Python 简单的银行系统

以下实例为学习 Python 银行系统的操作:

python 复制代码
means = [0, 0, 0]  
loan = 0
rate = 0
pay = 0
investment = 0
annual_rate = 0


# 计算定投预期收益
# 定投收益的计算公式为:M=a(1+x)[-1+(1+x)^n]/x;
# 其中M代表预期收益,a代表每期定投金额,x代表收益率,而n代表定投期数。
# 假设用户每月定投金额为300元,一年也就是3600元,年收益率为15%,
# 定投期限为35年,则可以计算出收益为3600(1+15%)[-1+(1+15%)^35]/15%=3648044元。
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("定投的预期收入为: %.2f" % expected)
    means[1] = expected
    return expected


def balance():
    total = 0
    for i in means:
        total += i
    print("你的资产总额为:%.2f" % total)
    print("你的资产明细为:\n")
    print("存款:%.2f" % means[0])
    print("理财:%.2f" % means[1])
    print("负债:%.2f" % means[2])


def saving(amount):
    global means
    if amount < 0:
        print("存款金额不可小于 0!")
    else:
        means[0] += amount
        print("已存款:%.2f 元" % amount)
        print("当前余额:%.2f 元" % means[0])


def draw_money(drawing):
    global means
    if drawing < 0:
        print("取款金额不可小于 0!")
    elif drawing > means[0]:
        print("取款金额不可超过余额!")
    else:
        means[0] -= drawing
        print("已取款: %.2f 元" % drawing)
        print("当前余额: %.2f 元" % means[0])


def loans(loan, rate, pay, years):
    global means
    if pay < (loan - pay) * rate:
        print("你是还不完的!!!")
    else:
        if years == 0:
            count = 0
            while loan > 0:
                loan -= pay
                loan *= (1 + rate)
                count += 1
            print("将在 %d 年后还完贷款。" % count)
        else:
            for _ in range(years):
                loan -= pay
                if loan == 0:
                    break
                else:
                    loan *= (1 + rate)
                    print("你现在的负债是: %.2f" % loan)
            # means[2] = loan
            return loan


# 未来财务状况
def future(years):
    income = fixed_investment(investment, annual_rate, years)
    debt = loans(loan, rate, pay, years)
    captial = means[0] + income - debt
    print("你第%i年的总资产有: %.3f" % (years, captial))


def init():
    print()
    print('''以下为可办理的业务:
        1. 查询资产
        2. 存款
        3. 取款
        4. 计算复利
        5. 计算贷款
        6. 计算未来资产
        q. 退出''')


def main():
    init()
    while True:
        choice = input("请输入您要办理的业务代码: ")
        #  查询余额
        if choice == "1":
            balance()
        # 存款
        elif choice == "2":
            inc = float(input("请输入存款金额: "))
            saving(inc)
        # 取款
        elif choice == "3":
            dec = float(input("请输入取款金额: "))
            draw_money(dec)
        # 计算定投
        elif choice == "4":
            investment = float(input("请输入每月定投金额: "))
            annual_rate = float(input("请输入年收益率: "))
            years = int(input("请输入定投期限(年): "))
            if investment <= 0 or annual_rate <= 0 or years <= 0:
                print("输入的数据有误")
            else:
                money = fixed_investment(investment, annual_rate, years)
            print("最终收获: %.2f 元" % money)
        # 计算贷款
        elif choice == "5":
            loan = float(input("请输入当前贷款: "))
            rate = float(input("请输入年利率: "))
            pay = float(input("请输入每年还款: "))
            if loan <= 0 or rate <= 0 or pay <= 0:
                print("输入的数据有误")
            else:
                loans(loan, rate, pay, 0)
        elif choice == "6":
            years = int(input("希望查询多少年后的财务状况? "))
            future(years)
        # 退出
        elif choice == "q":
            print("欢迎下次光临!再见!")
            break
        else:
            print("你输入的指令有误,请重新输入\n")


if __name__ == '__main__':
    main()

希望你也学会了,更多编程源码模板请来二当家的素材网:https://www.erdangjiade.com

相关推荐
YYuCChi17 分钟前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
南极星100518 分钟前
蓝桥杯JAVA--启蒙之路(十)class版本 模块
java·开发语言
baidu_2474386122 分钟前
Android ViewModel定时任务
android·开发语言·javascript
Dev7z34 分钟前
基于 MATLAB 的铣削切削力建模与仿真
开发语言·matlab
不能隔夜的咖喱40 分钟前
牛客网刷题(2)
java·开发语言·算法
VT.馒头41 分钟前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
前端玖耀里1 小时前
如何使用python的boto库和SES发送电子邮件?
python
serve the people1 小时前
python环境搭建 (十二) pydantic和pydantic-settings类型验证与解析
java·网络·python
小天源1 小时前
Error 1053 Error 1067 服务“启动后立即停止” Java / Python 程序无法后台运行 windows nssm注册器下载与报错处理
开发语言·windows·python·nssm·error 1053·error 1067
进击的小头1 小时前
实战案例:51单片机低功耗场景下的简易滤波实现
c语言·单片机·算法·51单片机