class Account:
    def __init__(self, name, balance=0):
        self.name = name
        self.balance = balance
        self.status = 'normal'

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if self.balance < amount:
            print('余额不足')
        else:
            self.balance -= amount

    def query_balance(self):
        print('你的余额是', self.balance)

    def lose_card(self):
        self.status = 'lost'

    def find_card(self):
        self.status = 'normal'

    def close_account(self):
        self.status = 'closed'

    @staticmethod
    def login(username, password):
        # 模拟用户名和密码的验证过程
        if username == 'user' and password == 'password':
            return True
        else:
            return False

def main():
    accounts = {}
    while True:
        print('欢迎来到银行')
        print('1. 开立账户')
        print('2. 存款')
        print('3. 取款')
        print('4. 查询余额')
        print('5. 挂失')
        print('6. 取消挂失')
        print('7. 销户')
        print('8. 退出')
        choice = input('请输入您的选择: ')
        if choice == '1':
            name = input('请输入您的姓名: ')
            balance = 0 # 默认开户时余额为0
            account = Account(name, balance)
            accounts[name] = account # 保存账户信息
            print('您的账户已开通')
        elif choice == '2':
            name = input('请输入您的姓名: ')
            amount = int(input('输入要存入的金额: '))
            account = accounts.get(name)
            if account:
                account.deposit(amount)
                print('您的存款已成功存入')
            else:
                print('账户不存在')
        elif choice == '3':
            name = input('请输入您的姓名: ')
            amount = int(input('请输入提取的金额: '))
            account = accounts.get(name)
            if account:
                account.withdraw(amount)
                print('您的取款成功了')
            else:
                print('账户不存在')
        elif choice == '4':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                account.query_balance()
            else:
                print('账户不存在')
        elif choice == '5':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                account.lose_card()
                print('您的卡片已丢失')
            else:
                print('账户不存在')
        elif choice == '6':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                account.find_card()
                print('您的卡片已找到')
            else:
                print('账户不存在')
        elif choice == '7':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                account.close_account()
                del accounts[name]
                print('您的帐户已关闭')
            else:
                print('账户不存在')
        elif choice == '8':
            break # 退出程序
        else:
            print('无效的选择')

if __name__ == '__main__':
    main()

代码功能:

  • 开户: 输入姓名,系统自动创建一个账户,初始余额为 0。
  • 存款: 输入姓名和存款金额,将金额存入账户。
  • 取款: 输入姓名和取款金额,从账户中提取金额。
  • 查询余额: 输入姓名,查询账户余额。
  • 挂失: 输入姓名,将账户状态设置为'lost',表示卡片丢失。
  • 取消挂失: 输入姓名,将账户状态设置为'normal',表示卡片已找回。
  • 销户: 输入姓名,关闭账户,并从系统中删除账户信息。

改进建议:

  1. 安全性和数据持久化: 该代码仅供学习参考,实际应用中需要考虑安全性,例如添加密码验证、数据持久化存储等。
  2. 用户界面: 可以考虑使用图形界面库,例如 Tkinter 或 PyQt,提升用户体验。
  3. 功能扩展: 可以增加其他功能,例如转账、利率计算、交易记录等。

学习要点:

  • 面向对象编程: 代码使用类 (Class) 和对象 (Object) 来模拟银行账户。
  • 方法: 类中的函数称为方法,用于实现账户的各种操作。
  • 属性: 类中的变量称为属性,用于存储账户信息,例如姓名、余额、状态等。
  • 字典: 使用字典来存储所有账户的信息。
  • 用户输入: 使用 input() 函数获取用户输入。
  • 条件语句: 使用 ifelif 语句判断用户选择并执行相应的操作。
  • 循环: 使用 while 循环实现程序的持续运行。

总结:

本代码简单演示了如何使用 Python 编写一个简单的银行账户管理系统,并介绍了面向对象编程的基本概念和技巧。通过学习本代码,可以进一步了解 Python 在实际应用中的应用场景。

Python 银行账户管理系统:模拟存款、取款、挂失等操作

原文地址: http://www.cveoy.top/t/topic/oSSl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录