欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用 python 函数做出一个简单小应用

程序员文章站 2022-07-03 20:32:37
学了几周的 Python,今天参考老师的教程,成功做出一个简单的“银行存取款系统”,算是一个小小的里程碑,很有成就感,以下是代码:balance = 1000account_log = []def valideate(func): def wrapper(*args,**kwargs): amount = str(args[0]) index = amount.index(".") if len(amount) - index - 1 >...

学了几周的 Python,今天参考老师的教程,成功做出一个简单的“银行存取款系统”,算是一个小小的里程碑,很有成就感,以下是代码:


balance = 1000
account_log = []

def valideate(func):
    def wrapper(*args,**kwargs):
        amount = str(args[0])
        index = amount.index(".")
        if len(amount) - index - 1 > 1:
            print("输入格式有误,小数点最多保留两位")
        else:
             func(*args,**kwargs)
    return wrapper

@valideate
def deposit(amount):
    """
    存款操作
    :param amount:存入金额
    :return:None
    """
    global balance
    balance += amount
    write_log(amount,"存入")

@valideate
def withdrawal(amount):
    """
    取款
    :param amount: 金额
    :return: None
    """
    global balance
    if amount > balance:
        print("余额不足")
    else:
        balance -= amount
    write_log(amount,"取出")

def write_log(amount,type):
    """
    写入日志
    :param amount: 金额
    :param type: 存入或取出
    :return:None
    """
    now = datetime.datetime.now()
    create_time = now.strftime("%Y-%m-%d %M:%H:%S")
    data = [create_time,type,amount,f"{balance:.2f}"]
    account_log.append(data)


def print_log():
    """
    查看流水
    :return:None
    """
    print(account_log)

def show_menu():
    menu = """
操作菜单:
0:退出;
1:存款;
2:取款;
3:查看记录;
    """
    print(menu)

while True:
    show_menu()
    num = int(input("请根据菜单编号输入:"))
    if num == 0:
        print("你已退出系统")
        break
    elif num == 1:
        print("存款")
        amount = float(input("请输入存款金额:"))
        deposit(amount)
        print(f"当前余额是{balance:.2f}")

    elif num == 2:
        print("取款")
        amount = float(input("请输入取款金额:"))
        withdrawal(amount)
        print(f"当前余额是{balance:.2f}")

    elif num == 3:
        print("查看记录")
        print_log()
    else:
        print("输入有误")

以下是输出结果:
用 python 函数做出一个简单小应用
输入相关数字,即可查看相应结果。

本文地址:https://blog.csdn.net/weixin_47385862/article/details/110247739