文章中主要涉及的方法是Python中的open(filename, ‘r')以讀的方式打開文件open(filename, ‘w')以寫的方式打開文件我們用for * in *讀取文件中的數(shù)據(jù)或者寫入文件數(shù)據(jù) 用dict(eval(list2))方法來把字符串轉(zhuǎn)化為字典。
import math
import re
def main(): # 主函數(shù)
select = True
while (select):
menu()
start_int = input("請選擇你您想要操作功能的序號:")
if start_int == "12":
select = False
print("你已經(jīng)退出系統(tǒng)歡迎下次在來")
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========銀行存取錢系統(tǒng)========")
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): # 創(chuàng)建文件方法
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 # 是否繼續(xù)添加
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("輸入無效,不是輸入整型數(shù),請重新輸入")
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() # 讀取全部內(nèi)容
for list in Bank:
d = dict(eval(list)) # 字符轉(zhuǎn)化為字典
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() # 讀取全部內(nèi)容
for list in Bank:
d = dict(eval(list)) # 字符轉(zhuǎn)化為字典
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)) # 字符轉(zhuǎn)化為字典
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("您的個人信息已經(jīng)注銷")
else:
print("請先登入!")
def revise(): # 修改個人賬戶方法
cz = []
global g_select
if g_select == 1:
username = input("請輸入您的用戶名:")
userpassword = input("請輸入您的密碼:")
file = open(filename, 'r')
Bank = file.readlines() # 讀取全部內(nèi)容
for list in Bank:
d = dict(eval(list)) # 字符轉(zhuǎn)化為字典
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)) # 字符轉(zhuǎn)化為字典
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() # 讀取全部內(nèi)容
for list in Bank:
d = dict(eval(list)) # 字符轉(zhuǎn)化為字典
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)) # 字符轉(zhuǎn)化為字典
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() # 讀取全部內(nèi)容
for list in Bank:
d = dict(eval(list)) # 字符轉(zhuǎn)化為字典
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)) # 字符轉(zhuǎn)化為字典
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("請你輸入整數(shù)年份!")
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__":