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()
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__":
运行图片:
以上是 python实现银行账户系统 的全部内容, 来源链接: utcz.com/z/358424.html