python实现银⾏账户系统
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()bank文件
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 = "" # 定义保存⽤户信息的⽂件名
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("请输⼊姓名")
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 = adlines() # 读取全部内容
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(('name')), ('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 = adlines() # 读取全部内容
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:
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 = adlines() # 读取全部内容
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 = adlines() # 读取全部内容
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 = adlines() # 读取全部内容
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__":
运⾏图⽚: