09-08-周一_15-39-37

This commit is contained in:
2025-09-08 15:39:37 +08:00
parent b21b9a72f3
commit 399e7d78e6
2 changed files with 49 additions and 13 deletions

View File

@@ -1222,3 +1222,51 @@ while True:
print("\033[41;37m输入错误\033[0m") print("\033[41;37m输入错误\033[0m")
``` ```
### 登录注册文件数据库版本
```python
while True:
# 获取文件中保存的用户名和密码信息
dic = {}
with open("db", "a+", encoding="utf-8") as f:
f.seek(0)
temp_data = f.readlines()
for i in temp_data:
data = i.strip()
if len(data) == 0:
continue
temp_db = data.split("✨")
dic[temp_db[0]] = temp_db[-1]
# 登录注册的逻辑
print("登录".center(30,"="))
print("请选择:\n1.登录\n2.注册")
choice = input(">>>")
if choice == "1":
username = input("输入用户名:")
if username in dic.keys():
password = input("输入密码:")
if dic[username] == password:
print("\033[42;30m 登录成功! \033[0m")
else:
print("\033[41;37m 密码错误 \033[0m")
else:
print("\033[41;37m用户名不存在\033[0m")
elif choice == "2":
username = input("输入用户名")
if username in dic.keys():
print("\033[34;41m 用户名已存在!\033[0m")
continue
password = input("请输入密码")
dic[username] = password
print("\033[42;30m 注册成功! \033[0m")
else:
print("\033[41;37m输入错误\033[0m")
# 将数据写入文件
with open("db", "w+", encoding="utf-8") as f:
for k,v in dic.items():
data = f"{k}{v}\n"
f.write(data)
```

View File

@@ -23,7 +23,7 @@ f.close()
```python ```python
f.close() f.close()
# 回收操作系统级打开的文件,系统级别不回收会导致文件一直被占用,如果程序以外崩溃,会导致 # 回收操作系统级打开的文件,系统级别不回收会导致文件一直被占用,如果程序以外崩溃,会导致数据丢失
del f del f
# 回收应用程序级的变量 # 回收应用程序级的变量
``` ```
@@ -276,28 +276,16 @@ os.rename('a.txt.new','a.txt')
文件a.txt内容每一行内容分别为商品名字价钱个数。 文件a.txt内容每一行内容分别为商品名字价钱个数。
apple 10 3 apple 10 3
tesla 100000 1 tesla 100000 1
mac 3000 2 mac 3000 2
lenovo 30000 3 lenovo 30000 3
chicken 10 3 chicken 10 3
通过代码,将其构建成这种数据类型:[{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......] 并计算出总价钱。 通过代码,将其构建成这种数据类型:[{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......] 并计算出总价钱。