10.7.2 datetime模块
对Time模块的补充
from datetime import datetime
[‘add’, ‘class’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’,
‘init_subclass’, ‘le’, ‘lt’, ‘ne’, ‘new’, ‘radd’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘rsub’, ‘setattr’, ‘sizeof’,
‘str’, ‘sub’, ‘subclasshook’, ‘astimezone’, ‘combine’, ‘ctime’, ‘date’, ‘day’, ‘dst’, ‘fold’,
‘fromisocalendar’, ‘fromisoformat’, ‘fromordinal’, ‘fromtimestamp’, ‘hour’, ‘isocalendar’,
‘isoformat’, ‘isoweekday’, ‘max’, ‘microsecond’, ‘min’, ‘minute’, ‘month’, ‘now’, ‘replace’, ‘resolution’,
‘second’, ‘strftime’, ‘strptime’, ‘time’, ‘timestamp’, ‘timetuple’, ‘timetz’, ‘today’, ‘toordinal’, ‘tzinfo’,
‘tzname’, ‘utcfromtimestamp’, ‘utcnow’, ‘utcoffset’, ‘utctimetuple’, ‘weekday’, ‘year’]
now() ------------获取当前的时间
datetime模块包含的类:
datetime模块包含的常量:
10.7.3 其他模块
日历的模块(calendar)
uuid 模块 ------------ 一般会用在文件上传或者文件备份的时候
产生一个永不重复的字符串
uuid.uuid4().hex
练习:完成用户注册登录的案例
import sys
import hashlib
users=[]
salt="34554efwfwcsa"
def main():
print("*~"*20)
print("1.用户注册")
print("2.用户登录")
print("3.退出系统")
print("*~"*20)
choice=input("请输入相对应的操作:")
return choice
def password_md5(password):
md5=hashlib.md5(password.encode("utf-8"))
md5.update(salt.encode("utf-8"))
return md5.hexdigest()
def register():
username=input("请输入用户名:")
password=input("请输入密码:")
if username==None or username.strip()=="":
print("用户名不能为空")
return
if password==None or password.strip()=="" or len(password)<6:
print("用户名不能为空或者小于6位")
return
for i in users:
if i.get("username")==username:
print("用户名重复")
return
user={}
user["username"]=username
# user["password"]=password
user["password"]=password_md5(password)
print(user)
users.append(user)
print(users)
def is_login(username,password):
for i in users:
if i.get("username")==username and i.get("password")==password:
print("登录成功")
return True
return False
def login():
username=input("请输入用户名")
password=input("请输入密码")
password=password_md5(password)
if is_login(username,password):
print("恭喜你,登录成功")
else:
print("登录失败")
while True:
choice=main()
if choice=="1":
print("用户注册")
register()
elif choice=="2":
print("用户登录")
login()
else:
print("退出系统")
sys.exit()