定义一个银行账户类

class Account: # 初始化方法,传入账户名和余额(默认为0),并设置账户状态为正常 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('Insufficient balance')
    else:
        self.balance -= amount

# 查询余额方法,输出当前余额
def query_balance(self):
    print('Your balance is', self.balance)

# 挂失方法,将账户状态设置为挂失
def lose_card(self):
    self.status = 'lost'

# 找回卡片方法,将账户状态设置为正常
def find_card(self):
    self.status = 'normal'

# 关闭账户方法,将账户状态设置为关闭,并从账户字典中删除该账户
def close_account(self):
    self.status = 'closed'

# 静态方法,模拟验证用户名和密码的过程,如果用户名和密码正确则返回True,否则返回False
@staticmethod
def login(username, password):
    if username == 'user' and password == 'password':
        return True
    else:
        return False

主函数

def main(): # 定义一个空的账户字典 accounts = {} while True: # 输出菜单 print('Welcome to the bank') print('1. Open account') print('2. Deposit') print('3. Withdraw') print('4. Check balance') print('5. Lose card') print('6. Find card') print('7. Close account') print('8. Exit') # 输入选项 choice = input('Enter your choice: ') # 根据选项执行相应的操作 if choice == '1': # 开户,输入账户名,设置初始余额为1000元,创建账户对象并保存到账户字典中 name = input('Enter your name: ') balance = 1000 account = Account(name, balance) accounts[name] = account print('Your account has been opened') elif choice == '2': # 存款,输入账户名和存款金额,查找账户并执行存款操作 name = input('Enter your name: ') amount = int(input('Enter the amount to deposit: ')) account = accounts.get(name) if account: account.deposit(amount) print('Your deposit has been successful') else: print('Account does not exist') elif choice == '3': # 取款,输入账户名和取款金额,查找账户并执行取款操作 name = input('Enter your name: ') amount = int(input('Enter the amount to withdraw: ')) account = accounts.get(name) if account: account.withdraw(amount) print('Your withdrawal has been successful') else: print('Account does not exist') elif choice == '4': # 查询余额,输入账户名,查找账户并执行查询余额操作 name = input('Enter your name: ') account = accounts.get(name) if account: account.query_balance() else: print('Account does not exist') elif choice == '5': # 挂失,输入账户名,查找账户并执行挂失操作 name = input('Enter your name: ') account = accounts.get(name) if account: account.lose_card() print('Your card has been lost') else: print('Account does not exist') elif choice == '6': # 找回卡片,输入账户名,查找账户并执行找回卡片操作 name = input('Enter your name: ') account = accounts.get(name) if account: account.find_card() print('Your card has been found') else: print('Account does not exist') elif choice == '7': # 关闭账户,输入账户名,查找账户并执行关闭账户操作 name = input('Enter your name: ') account = accounts.get(name) if account: account.close_account() del accounts[name] print('Your account has been closed') else: print('Account does not exist') elif choice == '8': # 退出程序 break else: # 无效选项 print('Invalid choice')

if name == 'main': main()

Python 银行账户系统模拟 - 开户、存款、取款、查询余额、挂失、找回卡片、关闭账户

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

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