一、文件修改、登录注册、tail工具
# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def change(file_path,old_str,new_str):
import os
with open(rf"{file_path}",mode="rt",encoding="utf-8") as f,
open(rf"new.txt",mode="wt",encoding="utf-8") as f1:
while True:
line = f.readline()
res = line.replace(old_str,new_str) # .replace()函数是返回一个新字符串,而非在原字符串上操作
if not line:
break
f1.write(res)
os.remove(rf"{file_path}")
os.rename(rf"new.txt",rf"{file_path}")
# 2、编写tail工具
def tail(file_path):
import time
import os
if os.path.exists(rf"{file_path}"):
with open(rf"{file_path}",mode="rb") as f:
f.seek(0,2)
while True:
line = f.readline()
if line:
print(line.decode("utf-8"),end="")
else:
time.sleep(0.5)
else:
with open(rf"{file_path}",mode="wb") as f:
pass
# 3、编写登录功能
def login():
import os
if os.path.exists("logined.txt"):
os.remove(r"logined.txt") # 文件必须存在 [WinError 2] 系统找不到指定的文件。: 'logined.txt'
username = input("请输入账号:")
password = input("请输入密码:")
with open(r"user_data.txt",mode="rt",encoding="utf-8") as f:
for line in f:
name, pwd = line.strip().split(":")
if name == username and password == pwd:
print("登录成功!")
with open(r"logined.txt",mode="wt",encoding="utf-8") as f1:
f1.write(rf"{username}")
return username
else:
print("用户名或密码错误,登录失败!")
# 4、编写注册功能
def register():
username = input("请输入注册账号:")
with open(rf"user_data.txt",mode="r+t",encoding="utf-8") as f:
for line in f:
name, pwd = line.strip().split(":")
if name == username:
print("用户已存在!")
break
else:
password = input("请设定密码:")
f.write(f"{username}:{password}
")
print(f"用户{username}注册成功!")
# 5、编写用户认证功能
def check(): # 查看是否登录成功,若成功,返回登录账号,若失败返回None
import os
if os.path.exists(r"logined.txt"):
with open(r"logined.txt",mode="rt",encoding="utf-8") as f:
return f.read()
else:
print("请登录!")
二、购物车
# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额
def print_screen():
print(" ATM服务 ".center(50,"-"),end="")
print('''
1、充值功能
2、转账功能
3、提现功能
4、查询余额功能
5、登录
(r:回到登录界面,q:退出程序)''')
print(" end ".center(50, "-"))
def search_name(file_path,name):
with open(file_path,mode="rt",encoding="utf-8") as f:
for line in f:
if line:
username, account = line.strip().split(":")
if name == username:
return line
else:
return
def charge(login_name):
num = input("请输入您的充值金额:")
line = search_name(r"db.txt",login_name)
if line:
user_name , account = line.strip().split(":")
account_num = float(account)
account_num += float(num)
account_num = str(account_num)
new_line = f"{login_name}:{account_num}
"
change("db.txt", line, new_line)
print(f"充值成功{num}元!您的账户余额为:{account_num}元")
else:
with open(r"db.txt",mode = "at",encoding="utf-8") as f:
f.write(f"{login_name}:{num}
")
print(f"充值成功{num}元!您的账户余额为:{num}元")
print("按Enter键继续。",end="")
def transfer(login_name):
apt_user = input("请输入您需要转账的账户:")
apt_line = search_name(r"db.txt",apt_user)
if apt_line:
num = input("请输入您需要转账的金额:")
num = float(num)
login_line = search_name(r"db.txt",login_name)
if not login_line:
with open(r"db.txt", mode="at", encoding="utf-8") as f:
f.write(f"{login_name}:{0}
")
login_line = f"{login_name}:{0}
"
g_user, account = login_line.strip().split(":")
account = float(account)
if account >= num:
a_user, apt_account = apt_line.strip().split(":")
apt_account = float(apt_account)
account -= num
apt_account += num
account = str(account)
apt_account = str(apt_account)
new_g = f"{g_user}:{account}
"
new_apt = f"{a_user}:{apt_account}
"
change(r"db.txt",login_line,new_g)
change(r"db.txt",apt_line,new_apt)
print(f"转账成功,您的账户余额为:{account}元。")
else:
print("余额不足,转账失败!")
else:
print("您需要转账的用户不存在!")
print("按Enter键继续。",end="")
def Cash(login_name):
num = input("请输入您需要提现的金额:")
num = float(num)
login_line = search_name(r"db.txt", login_name)
g_user, account = login_line.strip().split(":")
account = float(account)
if account >= num:
account -= num
account = str(account)
new_g = f"{g_user}:{account}
"
change(r"db.txt", login_line, new_g)
print(f"提现{num}元成功,您的账户余额为:{account}元。")
else:
print("余额不足,提现失败!")
print("按Enter键继续。",end="")
def check_balance(name):
line = search_name(r"db.txt",name)
if line:
user_name, account = line.strip().split(":")
print(f"账户余额为:{account}元")
else:
with open(r"db.txt",mode = "at",encoding="utf-8") as f:
f.write(f"{name}:{0}
")
print("账户余额为:0")
print("按Enter键继续。",end="")
import os
import time
tag = True
while tag:
while tag:
print(""*50)
print_screen()
command = input("请输入功能代码:")
if os.path.exists("logined.txt"):
if command == "1":
charge(name)
input("")
elif command == "2":
transfer(name)
input("")
elif command == "3":
Cash(name)
input("")
elif command == "4":
check_balance(name)
input("")
elif command == "5":
name = login()
elif command == "q" or command == "Q":
tag = False
os.remove("logined.txt")
elif command == "r" or command == "R":
os.remove("logined.txt")
break
else:
print("请先登录!")
time.sleep(1)