python实现银行账户系统
作者:nanxiang11 时间:2023-05-27 17:49:08
Python编写一个简易银行账户系统,供大家参考,具体内容如下
文章中主要涉及的方法是Python中的open(filename, ‘r')以读的方式打开文件open(filename, ‘w')以写的方式打开文件我们用for * in *读取文件中的数据或者写入文件数据 用dict(eval(list2))方法来把字符串转化为字典。
详细代码如下
import math
import re
def main(): # 主函数
select = True
while (select):
menu()
start_int = input("请选择你您想要操作功能的序号:")
if start_int == "12":
select = False
print("你已经退出系统欢迎下次在来")
elif start_int == "4":
insert()
elif start_int == "5":
login()
elif start_int == "6":
show()
elif start_int == "11":
delete()
elif start_int == "7":
revise()
elif start_int == "8":
deposit()
elif start_int == "9":
getMoney()
elif start_int == "10":
UseMoney()
def menu(): # 菜单显示
print("1========银行存取钱系统========")
print("2===========================")
print("3===========功能菜单===========")
print("4=========注册个人信息==========")
print("5============登入=============")
print("6=========查询个人信息==========")
print("7=========修改个人账户==========")
print("8============存钱=============")
print("9============取钱=============")
print("10=========显示年收益==========")
print("11========注销个人信息==========")
print("12===========退出=============")
filename = "Bank.txt" # 定义保存用户信息的文件名
def save(Bank): # 创建文件方法
try:
Bank_txt = open(filename, "a")
except Exception as e:
Bank_txt = open(filename, "w")
for info in Bank:
Bank_txt.write(str(info) + "\n")
Bank_txt.close()
def insert(): # 注册方法
BankList = [] # 保存用户信息列表
mark = True # 是否继续添加
while mark:
id = input("请输入您的ID密码(如1001):")
if not id:
break
name = input("请输入姓名")
if not name:
break
try:
deposit = int(input("输入你要存款的金额"))
if deposit == 0:
break
except:
print("输入无效,不是输入整型数,请重新输入")
continue
Bank = {"id": id, "name": name, "deposit": deposit}
BankList.append(Bank)
mark = False
save(BankList)
print("注册成功")
global g_select
g_select = 0
global Username
global Userpassword
def login(): # 登入方法
global Username
global g_select
global g_BankQuery
global Userpassword
g_BankQuery = []
Username = str(input("请输入您的用户名"))
Userpassword = str(input("请输入您的密码"))
file = open(filename, 'r')
Bank = file.readlines() # 读取全部内容
for list in Bank:
d = dict(eval(list)) # 字符转化为字典
if d['name'] == Username and d['id'] == Userpassword:
g_BankQuery.append(d)
print("登入成功!")
g_select = 1
else:
pass
if not g_BankQuery:
g_select = 0
print("登入失败请先注册!")
else:
pass
def show(): # 查询个人信息
if g_select == 1:
format_title = "{:^6}{:^12}"
print(format_title.format("名字", "存款"))
format_date = "{:^6}{:^12}"
for info in g_BankQuery:
print(format_date.format(str(info.get('name')), str(info.get('deposit'))))
else:
print("请先登入!")
def delete(): # 删除个人账户方法
global g_BankQuery
cz = []
global g_select
choose = 0
if g_select == 1:
while choose < 3:
username = str(input("请输入你姓名"))
userpassword = str(input("请输入您的密码"))
file = open(filename, 'r')
Bank = file.readlines() # 读取全部内容
for list in Bank:
d = dict(eval(list)) # 字符转化为字典
if d['name'] == username and d['id'] == userpassword:
cz.append(d)
file.close()
choose = 3
NewBank = open(filename, 'w') # 以写的方式打开文件
for list2 in Bank:
d2 = dict(eval(list2)) # 字符转化为字典
if d2['name'] != username and d2['id'] != userpassword:
NewBank.write(str(d2) + "\n")
else:
pass
else:
pass
if not cz:
choose = choose + 1
if choose == 3:
g_select = 0
print("请重新登入!")
else:
print("用户名或者密码错误,请重新输入你还有:" + str(3 - choose) + "机会")
else:
g_BankQuery.clear()
g_select = 0
print("您的个人信息已经注销")
else:
print("请先登入!")
def revise(): # 修改个人账户方法
cz = []
global g_select
if g_select == 1:
username = input("请输入您的用户名:")
userpassword = input("请输入您的密码:")
file = open(filename, 'r')
Bank = file.readlines() # 读取全部内容
for list in Bank:
d = dict(eval(list)) # 字符转化为字典
if d['name'] == username and d['id'] == userpassword:
cz.append(d)
file.close()
NewBank = open(filename, 'w') # 以写的方式打开文件
for list2 in Bank:
d2 = dict(eval(list2)) # 字符转化为字典
if d2['name'] == username and d2['id'] == userpassword:
d2['name'] = input("输入您的新名字:")
d2['id'] = input("输入您的新密码:")
NewBank.write(str(d2) + "\n")
print("修改成功,请重新登入!")
g_select = 0
else:
NewBank.write(str(d2) + "\n")
else:
pass
if not cz:
print("你输入的密码或者用户名有误请重新登入")
g_select = 0
else:
pass
else:
print("请先登入!")
def deposit(): # 存钱方法
global g_BankQuery
global g_select
cz = []
if g_select == 1:
money = int(input("请输入你要存多少钱:"))
file = open(filename, 'r')
Bank = file.readlines() # 读取全部内容
for list in Bank:
d = dict(eval(list)) # 字符转化为字典
if d['name'] == Username and d['id'] == Userpassword:
cz.append(d)
file.close()
NewBank = open(filename, 'w') # 以写的方式打开文件
for list2 in Bank:
d2 = dict(eval(list2)) # 字符转化为字典
if d2['name'] == Username and d2['id'] == Userpassword:
d2['deposit'] = str(int(d2['deposit']) + money)
NewBank.write(str(d2) + "\n")
print("储存成功!")
g_BankQuery.clear()
g_BankQuery.append(d2)
else:
NewBank.write(str(d2) + "\n")
else:
pass
else:
print("请先登入!")
def getMoney(): # 取钱方法
global g_select
global g_BankQuery
cz = []
if g_select == 1:
money = int(input("请输入你要取多少钱:"))
file = open(filename, 'r')
Bank = file.readlines() # 读取全部内容
for list in Bank:
d = dict(eval(list)) # 字符转化为字典
if d['name'] == Username and d['id'] == Userpassword:
cz.append(d)
if money > int(d['deposit']):
print("您的余额不足")
else:
file.close()
NewBank = open(filename, 'w') # 以写的方式打开文件
for list2 in Bank:
d2 = dict(eval(list2)) # 字符转化为字典
if d2['name'] == Username and d2['id'] == Userpassword:
d2['deposit'] = str(int(d2['deposit']) - money)
NewBank.write(str(d2) + "\n")
print("取钱成功!")
g_BankQuery.clear()
g_BankQuery.append(d2)
else:
NewBank.write(str(d2) + "\n")
else:
pass
else:
print("请先登入!")
def UseMoney(): # 利息计算
UM = True
while UM:
try:
money = float(input("请输入你要投资理财多少钱:"))
year = int(input("请你输入你要储存多少年:"))
except:
print("请你输入整数年份!")
if 0 < year <= 3:
profitmargin = 0.03
elif 3 < year <= 5:
profitmargin = 0.04
elif 5 < year <= 10:
profitmargin = 0.06
elif year > 10:
profitmargin = 0.08
if money < 0 or year <= 0:
print("您的本金不能少于0元或者年份不能少于0年")
else:
UM = False
profit = round(money * year * profitmargin, 3)
print("你储存:" + str(year) + "年将获得的利润会等于:" + str(profit) + "元本金加利润会等于:" + str(profit + money) + "元")
if __name__ =="__main__":
运行图片:
来源:https://blog.csdn.net/nanxiang11/article/details/112974649
标签:python,银行,系统
0
投稿
猜你喜欢
C#连接Oracle数据库使用Oracle.ManagedDataAccess.dll
2024-01-20 11:52:53
Python 工具类实现大文件断点续传功能详解
2022-11-17 05:41:29
python下载图片实现方法(超简单)
2021-12-21 02:17:07
Python编程使用DRF实现一次性验证码OTP
2021-07-30 00:25:26
python with (as)语句实例详解
2023-04-12 04:15:09
Python利用numpy实现三层神经网络的示例代码
2021-07-29 22:04:46
Python实现括号匹配方法详解
2021-05-01 23:06:58
多按钮共存——don’t make me think
2010-01-11 20:20:00
CSS样式设计技巧十则
2008-06-04 12:18:00
9个个人网站制作诀窍
2007-12-13 11:36:00
python使用SQLAlchemy操作MySQL
2024-01-16 19:11:11
Python快速生成随机密码超简单实现
2022-08-07 19:26:09
GOOGLE LOGO 设计演化过程
2008-02-13 19:41:00
python-docx修改已存在的Word文档的表格的字体格式方法
2021-07-20 05:03:07
asp程序错误详细说明例表
2008-04-02 12:13:00
Python import导入上级目录文件的方法
2022-11-16 01:27:41
Oracle 中文字段进行排序的sql语句
2024-01-22 13:26:43
Python脚本完成post接口测试的实例
2022-01-01 15:40:22
Python中文分词库jieba(结巴分词)详细使用介绍
2023-03-17 10:31:35
MySQL 存储过程中执行动态SQL语句的方法
2024-01-12 21:22:22