Python:函数综合案例-黑马ATM

发布时间 2023-12-08 23:46:13作者: hugh2023

综合案例:黑马ATM

  • 主菜单
  • 查询余额效果
  • 存取款效果
# 总额    total
total = 5000000
# 定义None影响不大,可以不定义
name = None

# 要求客户输入姓名
name = input("请输入您姓名:")


# 菜单提示
def menu():
    print("-" * 19 + "主菜单" + "-" * 19)
    print(f"{name},您好,欢迎来到黑马银行ATM,请选择操作:")
    print("查询余额\t\t【输入1】")
    print("存款\t\t\t【输入2】")
    print("取款\t\t\t【输入3】")
    print("退出\t\t\t【输入4】")
    return input("请输入您的选择:")


# 查询函数
def query(show_header):
    if show_header:
        print("-" * 18 + "查询余额" + "-" * 18)
    print(f"{name},您好,您的余额剩余:{total}")


# 取款函数
def withdraw(money):
    global total
    print("-" * 20 + "取款" + "-" * 20)
    if money > 0:

        if total > money:
            total -= money
            print(f"{name},您好,您取款{money}成功")
        else:
            print(f"{name},您好,您的余额不足,请重新取款")
    else:
        print("取款金额错误,请重新取款")
    # 调用query函数查询余额
    query(False)


# 存款函数
def deposit(money):
    global total

    print("-" * 20 + "存款" + "-" * 20)
    if money > 0:
        total += money
        print(f"{name},您好,您存款{money}成功")
    else:
        print("存款金额有误,请重新存款")
    # 调用query函数查询余额
    query(False)


while True:
    keyboard_input = menu()

    if keyboard_input == "1":
        query(True)
        continue
    elif keyboard_input == "2":
        money = int(input("请输入要存款的金额:"))
        deposit(money)
        continue

    elif keyboard_input == "3":
        money = int(input("请输入要取款的金额:"))
        withdraw(money)
        continue

    elif keyboard_input == "4":
        print("谢谢使用,系统已退出")
        break
    else:
        print("输入错误,请重新输入")
        continue