【 1 】明確我們要實現的功能
# ===================用戶功能菜單=====================
# ? ? ? ? ? ? ? ? ? 1.注冊
# ? ? ? ? ? ? ? ? ? 2.登陸
# ? ? ? ? ? ? ? ? ? 3.取款
# ? ? ? ? ? ? ? ? ? 4.轉賬
# ? ? ? ? ? ? ? ? ? 5.充值余額
# ? ? ? ? ? ? ? ? ? 6.查看流水
# ? ? ? ? ? ? ? ? ? 7.查看銀行信息(查看自己的卡號、余額、流水等信息)
# ======================歡迎使用=======================
?【 2 】注冊
我們可以用文件操作里面學到的直用文本讀寫我們要的信息
def enroll(username, password, age, gender, bankcard, withdraw_password, balance):with open('5.txt', 'a', encoding='utf-8') as f:f.write(f'{username}:{password}:{age}:{gender}:{bankcard}:{withdraw_password}:{balance}\n')
首先我們先定義一個enroll函數來定再用文件里的模式里的“a”追加功能
f.write(f'{username}:{password}:{age}:{gender}:{bankcard}:{withdraw_password}:{balance}\n')? #這個就是換行并且用:分隔
如果你要單獨實現的話就是這樣:
if xuan == 1:new_username = input('請輸入你的用戶名:')new_password = input('請輸入你的密碼:')new_age = input('請輸入你的年齡:')new_gender = input('請輸入你的性別:')new_bankcard = input('請輸入你的銀行卡號:')new_withdraw_password = input('請輸入你的提現密碼:')new_balance = input('請輸入你的余額: ')enroll(new_username, new_password, new_age, new_gender, new_bankcard, new_withdraw_password, new_balance)print('恭喜你注冊成功')
elif xuan == 0:print('退出程序')
else:print('無效的選擇')print('-------------------------------------------')
【 3 】登錄
?我們肯定要從文件里把數據讀取出來才能進行下一步操作:
def login(username, password):# 打開文件以讀取用戶名和密碼with open("5.txt", "r", encoding='utf-8') as file:for line in file:#遍歷values = line.strip().split(':')# 如果5.txt里面的文件字符不超過7就會觸發if len(values) < 7:print(f'文件格式錯誤,第{line}數據不完整')continue# 會先去除行末的換行符和空白符,然后按逗號分隔返回一個包含用戶名、密碼、性別和年齡的字符串列表。new_username, new_password, age, gender, bankcard, withdraw_password, balance = valuesif username == new_username and password == new_password:print("登錄成功!")print(f'用戶名:{new_username}')print(f'性別:{gender}')print(f'年齡:{age}')print(f'銀行卡:{bankcard}')print(f'銀行卡密碼:{withdraw_password}')print(f'余額:{balance}')return Trueprint("用戶名或密碼錯誤,請重新登錄。")return False
【 4 】取款
def withdraw(username, password, amount):# 打開文件讀取文本信息with open('5.txt', 'r', encoding='utf-8') as file:# 讀取每一行的信息lines = file.readlines()# 遍歷文件中的每一行,并將每一行的內容賦值給line,同時獲取每一行的索引賦值給ifor i, line in enumerate(lines):# 這行代碼將行文本按逗號分割后的部分存儲在values變量中,以便后續處理或使用。values = line.strip().split(':')if len(values) < 7:# print('文件格式錯誤,里面數據不完整')continue# 獲取用戶信息save_username, save_password, age, gender, bankcard, withdraw_password, balance = valuesif username == save_username and save_password == password:# 把余額轉化成小數類型balance = float(balance)if amount <= float(balance):# 余額總數減去要取的值balance = float(balance) - float(amount) # balance -= amountlines[i] = f"{save_username}:{save_password}:{age}:{gender}: {bankcard}:{withdraw_password}:{balance}\n"with open('5.txt', 'w', encoding='utf-8') as file:file.writelines(lines)print(f'請取走{amount} 元, 當錢余額為{balance} 元')else:print('窮逼還來取錢')return Trueprint('用戶名或密碼錯誤')return False
【 5 】轉賬
def transfer(second_username, second_password, jieshuo_username, amount):with open("5.txt", "r", encoding='utf-8') as file:# 讀取所有行的信息lines = file.readlines()# 遍歷文件的每一行for i, line in enumerate(lines):# 分割每一行的值values = line.strip().split(':')if len(values) < 7:continueusername, password, age, gender, bankcard, withdraw_password, balance = valuesif username == second_username and password == second_password:balance = float(balance)if balance >= float(amount):balance -= float(amount)lines[i] = f"{username}:{password}:{age}:{gender}:{bankcard}:{withdraw_password}:{balance}\n"breakelse:print("余額不足,無法完成轉賬。")return# 重新打開文件并寫入更新后的信息with open('5.txt', 'w', encoding='utf-8') as f:f.writelines(lines)# 增加接收者的余額with open('5.txt', 'r+', encoding='utf-8') as f:lines = f.readlines()# 其中enumerate() 函數用于同時獲取每一行的內容和行號,方便后續操作。line.split(':')則使用冒號分隔符將每行的數據分割成一個列表。for i, line in enumerate(lines):values = line.strip().split(':')# 讀取文件中的每一行,并將其解析成一個列表,如果列表長度小于7,則跳過這一行,不進行處理。if len(values) < 7:continueusername, password, age, gender, bankcard, withdraw_password, balance = valuesif username == jieshuo_username:balance = float(balance)balance += float(amount)# 更新用戶信息lines[i] = f"{username}:{password}:{age}:{gender}:{bankcard}:{withdraw_password}:{balance}\n"break# 最后將更新的信息寫回文件with open('5.txt', 'w', encoding='utf-8') as f:f.writelines(lines)print(f'轉賬成功!{amount} 元 用戶{second_username} 賬戶轉到{jieshuo_username}中')
【 6 】充值金額
def chongqian(username,password,amount):with open("5.txt", "r", encoding='utf-8') as file:# 讀取所有行的信息lines = file.readlines()# 遍歷文件的每一行for i, line in enumerate(lines):# 分割每一行的值values = line.strip().split(':')if len(values) < 7:continuesecond_username, second_password, age, gender, bankcard, withdraw_password, balance = valuesif username == second_username and password == second_password:balance = float(balance)if float(amount) <= 0:print('充值金額不能是負數:')return False# 更新余額信息balance += float(amount)new_lines = f"{second_username}:{second_password}:{age}:{gender}: {bankcard}:{withdraw_password}:{balance}\n"lines[i] = new_lines# 將更新后的用戶信息寫回文件中with open('5.txt', 'w', encoding='utf-8') as f:f.writelines(lines)print(f'充值成功!當前余額為{balance}人民幣')return Trueprint('用戶名或密碼錯誤')return False
【 7】 查看流水
from _datetime import datetime# 讀取保存用戶信息的文件,逐行讀取每個用戶的信息。
# 對于每個用戶,提取其操作記錄(例如轉賬、取款等)。
# 將操作記錄寫入一個新的文件中,并輸出到控制臺。
def chalook_water():# 打開文件并讀出所用行with open('5.txt', 'r', encoding='utf-8') as flie:lines = flie.readlines()# 遍歷每一行并打印出來,并添加時間for line in lines:timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 獲取當前時間print(f"{timestamp} {line.strip()}")# 使用文本的a的追加將流水時間寫入文件with open('5.log', 'a', encoding='utf-8') as f:f.write(f'{timestamp} {line.strip()}')print(timestamp,line.strip())
【 8?】查看銀行信息
?
def chalook_bankard(username, password, timestamp):with open('5.txt', 'r', encoding='utf-8') as f:for line in f:values = line.strip().split(":")if len(values) < 7:# print('文件格式錯誤,里面數據不完整')continuecha_username, cha_password, age, gender, bankcard, withdraw_password, balance = valuesif username == cha_username and password == cha_password:print(f'銀行卡卡號:{bankcard}')print(f'余額{balance}')with open('5.log', 'a', encoding='utf-8') as f:timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")# 生成當前時間戳,并使用f.write()向文件中寫入一行文本,文本內容包括時間戳、銀行卡號和余額信息。f.write(f'{timestamp} 銀行卡卡號:{bankcard} 余額:{balance}\n')return Trueprint("賬戶或密碼錯誤:")return False
【 9 】 主界面
? ? ???
flag = True
while True:print(''''雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務''')xuan = int(input('請選擇你想要的程序:'))if xuan == 1:username = input('請輸入你用戶名:')password = input('請輸入你密碼:')age = input('請輸入你年齡:')gender = input('請輸入你性別:')bankcard = input('請輸入你的銀行卡卡號:')withdraw_password = input('請輸入你的銀行卡密碼:')balance = input('請輸入你余額·:')enroll(username, password, age, gender, bankcard, withdraw_password, balance)if username is not None:print('恭喜你注冊成功')elif xuan == 2:username = input('請輸入你用戶名:')password = input('請輸入你密碼:')login(username, password)elif xuan == 3:username = input('請輸入用戶名:')password = input('請輸入密碼:')amount = int(input('請輸入取款金額:'))withdraw(username, password, amount)elif xuan == 4:flag = Falseprint('歡迎光臨')breakelif xuan == 5:second_username = input('請輸入你用戶:')second_password = input(('請輸入你的用戶密碼:'))jieshuo_username = input('請輸入你的接收用戶賬戶: ')amount = input('請輸入你要轉賬的金額: ')transfer(second_username, second_password, jieshuo_username, amount)elif xuan == 6:chalook_water()elif xuan == 7:username = input('請輸入用戶名: ')password = input('請輸入密碼:')timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")chalook_bankard(username, password, timestamp)elif xuan == 8:username = input('請輸入你要充值的用戶: ')password = input('請輸入你要充值的用戶的密碼: ')amount = input('請輸入你要充值的金額: ')chongqian(username,password,amount)else:print('無效的選項')
結果:
雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:1
請輸入你用戶名:mao
請輸入你密碼:123
請輸入你年齡:16
請輸入你性別:nan
請輸入你的銀行卡卡號:6666
請輸入你的銀行卡密碼:1111
請輸入你余額·:500000
---------------------------------
恭喜你注冊成功
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:1
請輸入你用戶名:tian
請輸入你密碼:123
請輸入你年齡:19
請輸入你性別:nv
請輸入你的銀行卡卡號:5555
請輸入你的銀行卡密碼:1111
請輸入你余額·:50000000
---------------------------------
恭喜你注冊成功
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:1
請輸入你用戶名:tian
請輸入你密碼:123
請輸入你年齡:18
請輸入你性別:nan
請輸入你的銀行卡卡號:7777
請輸入你的銀行卡密碼:1111
請輸入你余額·:50000
---------------------------------
恭喜你注冊成功
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:3
請輸入用戶名:tian
請輸入密碼:123
請輸入取款金額:2000
請取走2000 元, 當錢余額為108211.2 元
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:5
請輸入你用戶:mao
請輸入你的用戶密碼:123
請輸入你的接收用戶賬戶: tian
請輸入你要轉賬的金額: 1000
轉賬成功!1000 元 用戶mao 賬戶轉到tian中
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:7
請輸入用戶名: mao
請輸入密碼:123
銀行卡卡號: 3333
余額147888.7
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:8
請輸入你要充值的用戶: mao
請輸入你要充值的用戶的密碼: 123
請輸入你要充值的金額: 600000
充值成功!當前余額為747888.7人民幣
'雙面龜的日常生活1.注冊2.登陸3.取款4.退出5.轉賬6.查看流水7.查看銀行信息8.充值業務請選擇你想要的程序:4
歡迎光臨