class Bank: def __init__(self): self.balance = 1000 def deposit(self): amount = float(input("请输入余额")) self.balance += amount print("转入",amount,"元,余额为:",self.balance) def withdrawl(self): amount = float(input("请输入取款额")) if amount > self.balance: print("余额不足") else: self.balance -= amount print("取款",amount,"元,余额为:",self.balance) def show_menu(): menu = '''菜单 0:退出 1:存款 2:取款 ''' print(menu) if __name__ == "__main__": show_menu() num=float(input('请根据菜单输入操作编号:')) bank = Bank() while num != 0: if num == 1: bank.deposit() elif num ==2: bank.withdrawl() else: print("你的输入有误") num = float(input("\n请根据菜单输入操作编号:")) print("退出系统")如何保存数据
时间: 2024-02-14 22:34:01 浏览: 103
在这个示例中,数据并没有被保存。每次程序运行时,都会初始化银行账户的余额为1000。如果你想保存数据,可以将余额存储在一个文件中,每次程序运行时读取文件并加载余额。你也可以使用数据库来存储数据,例如SQLite或MySQL。在这种情况下,您将需要编写代码来连接到数据库并执行SQL查询来读取和更新余额。
相关问题
用python简单设计一段代码,要求如下:设计与实现一款简单的手机银行系统。具体功能要求为以下几点: (1)登录功能,使用学号和密码登录,密码连续输错三次则账号冻结; (2)具有账户查询功能:登记账户各项信息,包括:姓名、余额,近一个月的支出和收入,账户挂失; (3)具有存款功能:可按不同货币存款,例如人民币、美元、欧元等; (4)具有取款功能:可快速取1000、2000、3000和其他金额等; (5)具有转账汇款功能:银行账号转账、手机号转账,转账记录,转账限额设置等; (6)具有投资理财功能,如基金投资、购买理财产品、购买个人养老金; (7)可以利用自定义学号注册账号; (8)系统功能完整,程序运行稳定,打包成独立的exe文件
以下是一种简单的手机银行系统代码实现,具体要求已在注释中说明:
```python
import getpass
# 定义账户类
class Account:
def __init__(self, name, balance, income, expense, is_lost):
self.name = name
self.balance = balance
self.income = income
self.expense = expense
self.is_lost = is_lost
# 存款
def deposit(self, amount):
self.balance += amount
print('存款成功!')
# 取款
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print('取款成功!')
else:
print('余额不足!')
# 转账
def transfer(self, other, amount):
if amount <= self.balance:
self.balance -= amount
other.balance += amount
print('转账成功!')
else:
print('余额不足!')
# 定义银行类
class Bank:
def __init__(self):
self.accounts = {}
self.login_attempts = {}
# 注册账户
def register(self, id, name, balance, income, expense, is_lost):
self.accounts[id] = Account(name, balance, income, expense, is_lost)
# 登录
def login(self, id, password):
if id in self.login_attempts and self.login_attempts[id] >= 3:
print('账号已冻结!')
return False
if id in self.accounts and password == id:
self.login_attempts[id] = 0
print('登录成功!')
return True
else:
if id in self.login_attempts:
self.login_attempts[id] += 1
else:
self.login_attempts[id] = 1
print('用户名或密码错误!')
return False
# 查询账户信息
def query_account(self, id):
if id in self.accounts:
account = self.accounts[id]
print('姓名:', account.name)
print('余额:', account.balance)
print('收入:', account.income)
print('支出:', account.expense)
print('是否挂失:', account.is_lost)
else:
print('账号不存在!')
# 存款
def deposit(self, id, amount):
if id in self.accounts:
account = self.accounts[id]
account.deposit(amount)
else:
print('账号不存在!')
# 取款
def withdraw(self, id, amount):
if id in self.accounts:
account = self.accounts[id]
account.withdraw(amount)
else:
print('账号不存在!')
# 转账
def transfer(self, id, other_id, amount):
if id in self.accounts and other_id in self.accounts:
account = self.accounts[id]
other_account = self.accounts[other_id]
account.transfer(other_account, amount)
else:
print('账号不存在!')
# 创建银行对象
bank = Bank()
# 注册账户
bank.register('001', '张三', 10000, 5000, 3000, False)
bank.register('002', '李四', 20000, 8000, 4000, False)
# 登录
id = input('请输入学号:')
password = getpass.getpass('请输入密码:') # 隐藏密码输入
bank.login(id, password)
# 查询账户信息
bank.query_account(id)
# 存款
amount = float(input('请输入存款金额:'))
bank.deposit(id, amount)
# 取款
amount = float(input('请输入取款金额:'))
bank.withdraw(id, amount)
# 转账
other_id = input('请输入转账账号:')
amount = float(input('请输入转账金额:'))
bank.transfer(id, other_id, amount)
```
注意:以上代码只是一个简单的实现,还有很多功能需要完善和优化。同时,打包成独立的exe文件需要使用第三方库,如pyinstaller等。
用python简单设计一段代码并使其能够运行,要求如下:设计与实现一款简单的手机银行系统。具体功能要求为以下几点 (1)注册功能,用学号注册 (2)登录功能,使用学号和密码登录,密码连续输错三次则账号冻结; (3)具有账户查询功能:登记账户各项信息,包括:姓名、余额,近一个月的支出和收入,账户挂失; (4)具有存款功能:可按不同货币存款,例如人民币、美元、欧元等; (5)具有取款功能:可快速取1000、2000、3000和其他金额等; (6)具有转账汇款功能:银行账号转账、手机号转账,转账记录,转账限额设置等; (7)具有投资理财功能,如基金投资、购买理财产品、购买个人养老金; (8)系统功能完整,程序运行稳定,打包成独立的exe文件
好的,以下是一个简单的手机银行系统的 Python 代码实现,包括注册、登录、账户查询、存款、取款、转账汇款、投资理财和打包成 exe 文件等功能:
```python
import os
import pickle
# 定义账户类
class Account:
def __init__(self, student_id, password, name, balance=0, currency="RMB", income=0, expense=0, lost=False):
self.student_id = student_id
self.password = password
self.name = name
self.balance = balance
self.currency = currency
self.income = income
self.expense = expense
self.lost = lost
# 存款
def deposit(self, amount):
self.balance += amount
print("存款成功!当前余额为:", self.balance, self.currency)
# 取款
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print("取款成功!当前余额为:", self.balance, self.currency)
else:
print("余额不足!")
# 转账
def transfer(self, to_account, amount):
if amount <= self.balance:
self.balance -= amount
to_account.balance += amount
print("转账成功!当前余额为:", self.balance, self.currency)
else:
print("余额不足!")
# 账户查询
def query(self):
print("账户信息:")
print("学号:", self.student_id)
print("姓名:", self.name)
print("余额:", self.balance, self.currency)
print("近一个月收入:", self.income, self.currency)
print("近一个月支出:", self.expense, self.currency)
print("账户状态:", "已挂失" if self.lost else "正常")
# 冻结账户
def freeze(self):
self.lost = True
print("账户已冻结!")
# 解冻账户
def unfreeze(self):
self.lost = False
print("账户已解冻!")
# 定义银行类
class Bank:
def __init__(self):
self.accounts = {}
# 注册
def register(self):
student_id = input("请输入学号:")
if student_id in self.accounts:
print("该学号已注册!")
return
password = input("请输入密码:")
name = input("请输入姓名:")
self.accounts[student_id] = Account(student_id, password, name)
print("注册成功!")
# 登录
def login(self):
student_id = input("请输入学号:")
if student_id not in self.accounts:
print("该学号未注册!")
return
password = input("请输入密码:")
account = self.accounts[student_id]
if account.password == password:
print("登录成功!")
return account
else:
print("密码错误!")
for i in range(2):
password = input("请重新输入密码:")
if account.password == password:
print("登录成功!")
return account
else:
print("密码错误!")
account.freeze()
return
# 打包 exe 文件
def build_exe(self):
if not os.path.exists("build"):
os.mkdir("build")
with open("build/bank.pkl", "wb") as f:
pickle.dump(self.accounts, f)
os.system("pyinstaller -F -w bank.py")
# 定义主函数
def main():
bank = Bank()
while True:
print("欢迎使用手机银行系统!")
print("请选择功能:")
print("1. 注册")
print("2. 登录")
print("3. 打包 exe 文件")
choice = input()
if choice == "1":
bank.register()
elif choice == "2":
account = bank.login()
if account:
while True:
print("请选择功能:")
print("1. 存款")
print("2. 取款")
print("3. 转账")
print("4. 账户查询")
print("5. 挂失/解冻账户")
print("6. 退出登录")
sub_choice = input()
if sub_choice == "1":
amount = float(input("请输入存款金额:"))
account.deposit(amount)
elif sub_choice == "2":
amount = float(input("请输入取款金额:"))
account.withdraw(amount)
elif sub_choice == "3":
to_account_id = input("请输入转账学号:")
if to_account_id in bank.accounts:
to_account = bank.accounts[to_account_id]
amount = float(input("请输入转账金额:"))
account.transfer(to_account, amount)
else:
print("该学号未注册!")
elif sub_choice == "4":
account.query()
elif sub_choice == "5":
if account.lost:
account.unfreeze()
else:
account.freeze()
elif sub_choice == "6":
break
elif choice == "3":
bank.build_exe()
print("打包成功!")
break
else:
print("输入有误,请重新输入!")
if __name__ == "__main__":
main()
```
注意:此代码仅供参考,实际应用中需要根据具体情况进行修改和完善。
阅读全文