文章目录
-
- 第一题:文章管理系统
- 第二题:使用面向对象实现一个栈
第一题:文章管理系统
1、用户管理系统
2、使用密码加密
3、时间API
import pickle
from re import T
import os
import hashlib
import datetime
from secrets import choice
import sys
import hashutil as hashutil
from os import path
users = []
articles = []
url = "users.dat"
def menu1():
print("~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ")
print("\t\t 1、注册用户\n")
print("\t\t 2、登录用户\n")
print("\t\t 3、退出系统\n")
print("~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ~ * ~ ")
return input("请输入您要选择的操作:")
def exists_user_by_username(username):
for user in users:
if user.get("username") == username:
return True
return False
def check_user_info(username, password):
for user in users:
if user.get("username") == username:
new_pass = hashutil.encryption_by_md5(password)
if user.get("password") == new_pass:
return True
else:
print("对不起,密码错误")
return False
return False
def register():
while True:
username = input("请输入您的账号名称:").strip()
password = input("请输入您的账号密码:").strip()
confirm_pass = input("请再次输入您的账号密码:").strip()
if exists_user_by_username(username):
print("对不起,该用户名称已经存在,请重新注册!!")
else:
if len(password) < 3:
print("对不起,密码长度不能少于3位")
else:
if password != confirm_pass:
print("对不起,两次密码不一致!!!")
else:
new_pass = hashutil.encryption_by_md5(password)
user = {"username": username, "password": new_pass, "age": 18}
users.append(user)
print("恭喜您,注册成功")
pickle.dump(users, open(url, "bw"))
break
def login():
username = input("请输入您的账号名称:").strip()
password = input("请输入您的账号密码:").strip()
if check_user_info(username, password):
print("恭喜您,登录成功")
print("进入二级文章页面")
def NewTxt():
global ActUser
title = input("请输入文章标题:")
file_path = "./" + ActUser + "/"
isExist = os.path.exists("./" + ActUser)
if isExist == False:
os.mkdir("./" + ActUser)
os.chdir("./" + ActUser)
else:
os.chdir("./" + ActUser)
file = open(title + ".txt","w")
file.write("标题:" + title + "\r\n")
file.write("时间:" + datetime.datetime.now().strftime('%Y-%m-%d') + "\r\n")
print("请输入文章内容[单独输入'q'保存退出]:")
while True:
content = input()
if content != 'q':
file.write("%s\n"%content)
else:
break
file.close()
print("文章已保存")
os.chdir("..")
return True
def ShowTxt():
global ActUser
isExist = os.path.exists("./" + ActUser)
if isExist == False:
os.mkdir("./" + ActUser)
os.chdir("./" + ActUser)
else:
os.chdir("./" + ActUser)
dirs = os.listdir(os.getcwd())
print("\n\n\n\n")
print("文章列表:")
for file in dirs:
print(file)
os.chdir("..")
def DelTxt():
global ActUser
isExist = os.path.exists("./" + ActUser)
if isExist == False:
os.mkdir("./" + ActUser)
os.chdir("./" + ActUser)
else:
os.chdir("./" + ActUser)
txt = input("请输入要删除的文章标题")
if isExist == os.path.exists(txt + ".txt"):
os.remove(txt + ".txt")
print("删除成功!")
os.chdir("..")
return True
else:
print("文章不存在!请重新输入")
os.chdir("..")
DelTxt()
def txt():
ShowTxt()
print("******************************")
print("\t\t 1、新建文章\n")
print("\t\t 2、查看文章\n")
print("\t\t 3、删除文章\n")
print("\t\t 4、退出登录\n")
while True:
i = input("请选择要进行的操作:")
if i == '1':
NewTxt()
break
elif i == '2':
ShowTxtContent()
break
elif i == '3':
DelTxt()
break
elif i == '4':
return True
def ShowTxtContent():
global ActUser
isExist = os.path.exists("./" + ActUser)
if isExist == False:
os.mkdir("./" + ActUser)
os.chdir("./" + ActUser)
else:
os.chdir("./" + ActUser)
txt = input("请输入要查看的文章的标题")
file = open(txt + ".txt")
line = file.readline()
print("\n\n\n\n")
while line:
print(line,end='')
line = file.readline()
file.close()
os.chdir("..")
if __name__ == '__main__':
if path.exists(url):
users = pickle.load(open(url, "br"))
while True:
choice = menu1()
print(users)
if choice == "1":
register()
elif choice == "2":
login()
else:
sys.exit()
第二题:使用面向对象实现一个栈
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
def push(self, item):
self.items.append(item)
def pop(self, item):
return self.items.pop(item)
if __name__ == '__main__':
my_stack = Stack()
my_stack.push('1')
my_stack.push('2')
my_stack.push('3')
my_stack.push('5')
print(my_stack.size())
print(my_stack.peek())
print(my_stack.pop())
print(my_stack.peek())
print(my_stack.size())
print(my_stack.pop())
print(my_stack.size())
print(my_stack.is_empty())