购物车终极版
购物车终极版
#需求:1.可自由管理入购物车
# 2.当浏览商品时,显示当前页码,以及共几页
# 3.设计一个返回的操作,即返回上一层函数,或直接退出的选项
# 4.先登录系统或者注册才能继续后续操作
# 5.商品浏览时,可选择全部浏览或者分页浏览
# 6.做一个搜索框,可以搜索需要的商品
# 7.用户名输错三次便强制退出,1分钟后才可以继续输入,并显示剩余可输入的次数
# 8.对管理员和用户做区分
def blacklist():
'''
拉进黑名单的操作
:return:
pass
'''
name = input("请输入你要拉黑的姓名:")
with open("dongjie.txt",'a',encoding = "utf-8")as f:
f.write(name)
f.close()
def serch():
'''
商品搜索
:return:
'''
while True:
select = input("请输入要搜索的内容(n返回):")
if select.upper() == "N":
return run()
with open("zidian",'r',encoding="utf-8")as f:
val = f.readlines()
lis = [item.strip("\n") for item in val]
flag = False
for i in lis:
v = eval(i)
if v.get("title") == select:
flag = True
print(v)
if not flag:
print("所搜索的内容不存在,请重新输入吧")
continue
def log():
'''
用户登录
:return:
部分返回主函数
'''
count = 1
import time
while True:
print("***************用户登录***************")
user_name = input("请输入用户名(n返回):")
if user_name.upper() == "N":
return run()
user_pwd = input("请输入登录密码:")
count = count + 1
with open("dongjie.txt", 'r', encoding='utf-8') as f:
user_info = f.readlines()
new_user_info = [item.strip() for item in user_info if item.strip()] # strip与if的搭配也要值得注意
if user_name in new_user_info:
print("你的账户已被冻结,强制退出")
return
f.close()
user_exit = False
flag = False
with open("zhuce", "r", encoding="utf-8") as f1:
for line in f1:
user_name1, user_pwd1, time1 = line.split("|")
#与注册的格式有关,应先注册
if user_name == user_name1:
user_exit = True
if user_name == user_name1 and user_pwd == user_pwd1:
flag = True
print("你已经登入系统")
choice = input("请选择接下来的服务类型(n返回)\n(1:商品浏览 2:我的购物车 3:搜索商品):")
if choice.upper() == "N":
return log()
num = int(choice)
dic = {1: browse, 2: shopping_car, 3:serch}
func = dic.get(num) #调用次一级函数
func()
if count > 3:
print("登录次数已用完,账户冻结1分钟,1分钟后重新输入")
time.sleep(60) #账户冻结60秒
continue
if not user_exit:
print("用户名不存在,请重新输入,剩余登录次数%s"%(4-count))
continue
if not flag:
print("用户名或密码错误,剩余登录次数%s"%(4-count))
def register():
'''
用户注册
:return:
部分返回主函数
'''
from datetime import datetime
print("---------用户注册-----------")
while True:
user_name = input("请输入用户名(N返回):")
if user_name.upper() == "N":
return run()
user_pwd = input("请输入登录密码:")
creat_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("zhuce", 'a', encoding='utf-8') as f:
line = "%s|%s|%s\n" % (user_name, user_pwd, creat_time)
f.write(line)
f.close()
def browse():
'''
商品浏览
:return:
部分返回主函数
'''
def select1():
'''
全局浏览
:return:
pass
'''
with open("zidian", 'r', encoding='utf-8') as f:
result = f.readlines()
val = [item.strip("\n") for item in result]
for i in val:
print(i)
def select2():
'''
分页浏览
:return:
pass
'''
with open("zidian", 'r', encoding='utf-8') as f:
lis = []
count = 0
count_num = len(f.readlines()) # 生成一个计数器
number = count_num // 4 + 1
f.seek(0)
while True:
choice = input("请输入要浏览的页码(n返回):") # 大于0切合法
if choice.upper() == "N":
return browse()
num = int(choice)
if num > number:
print("最大页码为%s,请重新输入" % number)
continue
star_info = (num - 1) * 4
end_info = num * 4
for line in f:
count += 1
if count > star_info:
lis.append(line)
if count >= end_info:
break
print("当前页码为%s,共%s页" % (num, number), )
for i in lis:
val = i.strip("\n")
print(val)
f.close()
select = input("请选择你的浏览方式(1.全局浏览 2.分页浏览):")
dic = {"1":select1,"2":select2}#触发不同的函数
val = dic.get(select)
val()
def shopping_car():
'''
我的购物车
:return:
pass
'''
# 购物车
SHOPPING_CAR = {}
GOODS_LIST = []
with open("zidian", 'r', encoding ="utf-8") as f:
for item in f:
GOODS_LIST.append(eval(item))
###需要加装饰器
for i in range(len(GOODS_LIST)):
print(i + 1, GOODS_LIST[i]["title"])
while True:
choice = input("请选择商品的序列号(n返回):")
if choice.upper() == "N":
return run()
num = int(input("请选择需要商品的数量:"))
row_info = GOODS_LIST[int(choice) - 1]
if row_info["id"] in SHOPPING_CAR: # 标志物
SHOPPING_CAR[row_info["id"]]["count"] += num # 需要定义SHOPPING_CAR的类型
else:
SHOPPING_CAR[row_info['id']] = {"title": row_info["title"], "price": row_info["price"],
"count": num} # 键值对的生成
print(SHOPPING_CAR)
def see_white():
with open("zhuce","r",encoding="utf-8")as f:
result = f.read()
print(result)
f.close()
def see_black():
with open("dongjie.txt","r",encoding="utf-8")as f:
result = f.read()
print(result)
f.close()
def adm():
'''
管理员程序主接口
:return:
pass
'''
question = input("口令?(提示:who is u dad?):")
if question == "杨子良":
chioce = input("请选择服务类型(n退出程序):1.查看用户信息 2.查看黑名单 3.增加黑名单:")
if chioce.upper() == "N":
return
num = int(chioce)
dic = {1: see_white, 2: see_black,3:blacklist }
func = dic.get(num)
func()
def run():
'''
用户主接口
:return:
部分退出程序
'''
print("1:用户登录,2:用户注册")
chioce = input("请选择服务的序号(n退出程序):")
if chioce.upper() == "N":
return
num = int(chioce)
dic = {1:log,2:register,}
func = dic.get(num)
func()
def main():
'''
程序入口
:return:
'''
print("*****************************欢迎来到我的购物商城*****************************")
select = input("你是?1:客户 2:管理员 :" ) #发出询问,辨别身份
dic = {"1":run,"2":adm}
func = dic.get(select)
func()
if __name__ == '__main__':
main()
总结:
- 优点:
- 要求基本完成,而且内容相对完备
- 缺点:
- 不够高级。基本还是函数的堆砌,没有用到装饰器之类的
记录学习的点点滴滴