博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
购物车升级版本
阅读量:4954 次
发布时间:2019-06-12

本文共 8511 字,大约阅读时间需要 28 分钟。

def man():     while 1:         choise = input("[1]登录 [2]注册 [3]购物 [4]退出 请选择:>>")         if choise == "1":             login()         elif choise == "2":             register()         elif choise == "3":             print("请先登录,再进行购物".center(15,"*"))             login()         elif choise == "4":             print("已退出系统".center(15,"*"))             break         else:             print("输入有误重新输入") def register():     freg = open('pd_file', 'a+', encoding='utf-8')     freg.seek(0)     users_info = {}  # 定义存放用户名,密码的 字典     for line in freg.readlines():        # 逐行读取用户名,密码,并存储到字典中         users = line.strip().split(',')         users_info[users[0]] = users[1]     while True:         username = input('请注册用户名:>>').strip()         if username in users_info.keys():             print('用户名已存在,请重新注册')             man()         else:             password = input('请输入密码:').strip()  # 确认两次密码输入             cpasswd = input('请确认密码:').strip()             if username and password and cpasswd:                 if password != cpasswd:                     print('两次输入密码不一致,请重新注册!')                     register()                 else:                     freg.write(username + ',' + password + '\n') # 写入密码文件                     print('注册成功')                     break     freg.close() # def login(): #     with open('pd_file_1', 'r', encoding='utf-8') as f: #         users_info = {} #         username = input('请输入用户名:>>') #         for line in f.readlines(): #             users = line.strip().split(',') #             users_info = {"name": users[0], "pws": users[1]}  # 循环获取 帐号和密码 #             # print("用户名:%s  密码%s" % (users_info['name'], users_info['pws']))  # 用户名 密码 #             # print(users_info["pws"])   # 密码 #             if username == users_info["name"]:  # 判断用户是否存在 #                 while 1: #                     password = input('请输入密码:') #                     if password == users_info["pws"]:  # 判断用户密码是否正确 #                         print("登录系统成功".center(15,"*")) #                         shoping() #                     else: #                         n = 3 #                         while n > 0:  # 三次错误密码循环 #                             n -= 1 #                             password = input('用户或密码错误,请重新输入,还有%s机会:>>' % n) #                             if n == 1: #                                 print("用户或密码错误,注册或重新登录") #                                 man() #                     break #         else: #             flog = "不存在" # 标记用 #         if flog == "不存在": #             print("用户不存在,请注册") #             man() def login():     with open('pd_file', 'r', encoding='utf-8') as f:         users_info = {}         username = input('请输入用户名:>>')         for line in f.readlines():             users = line.strip().split(',')             users_info = {"name": users[0], "pws": users[1]}  # 循环获取 帐号和密码             # print("用户名:%s  密码%s" % (users_info['name'], users_info['pws']))  # 用户名 密码             # print(users_info["pws"])   # 密码             if username == users_info["name"]:  # 判断用户是否存在                 while 1:                     password = input('请输入密码:>>')                     if password == users_info["pws"]:  # 判断用户密码是否正确                         print("登录系统成功".center(15,"*"))                         shoping()                     else:                         n = 3                         while n > 0:  # 三次错误密码循环                             n -= 1                             print('用户或密码错误,请重新输入,还有%s机会' % n)                             password = input('请输入密码:>>')                             if password == users_info["pws"]:  # 判断用户密码是否正确                                 print("登录系统成功".center(15, "*"))                                 shoping()                             if n == 1:                                 print("用户或密码错误,注册或重新登录")                                 man()                     break         else:             flog = "不存在" # 标记用         if flog == "不存在":             print("用户不存在,请注册")             man() def shoping():     product_lst = [         {"name": " 电脑 ", "price": 1999,"ID":0},         {"name": " 鼠标 ", "price": 10,"ID":1},         {"name": " 游艇 ", "price": 20,"ID":2},         {"name": " 美女 ", "price": 998,"ID":3}]     shop_car = []     cost_money = 0     ID_lst = []     print("欢迎光临幸福超市".center(15,"*"))     while 1:         salary = input("输入金额进行购物:>>")         if salary.isdigit():             salary = int(salary)             break         else:             print("输入金额有误,请重新输入")     chajia = 0     while 1:         print("买商品信息如下".center(20, "*"), "\nID", "商品名称".center(9, " "), "价格")         for i in range(len(product_lst)):             print(i, product_lst[i]["name"].center(11, " "), product_lst[i]["price"])  # 输出商品列表信息             # print(i + 1, product_lst[i]["name"].center(11, " "), product_lst[i]["price"])  # 输出商品列表信息         choise = input("输入ID将商品加入购物车,[N]结算 [Q]退出:>>")         if choise.isdigit():  # 开始购物             index = int(choise)  # 购物车的 ID 下标             if int(index) < 0 or int(index) >= len(product_lst):                 print("选择有误,重新选择:>>")                 continue             else:                 for item in shop_car:                     if item["ID"] == index:                         item["totle"] += 1                         ID_lst.append(product_lst[index]["ID"])                         break                 else:  # 第一次商品加入购物车 增加ID项  和 数量项                     shop_car.append({"ID": index, 'name': product_lst[index]['name'], "price": product_lst[index]['price'],"totle": 1})                     ID_lst.append(product_lst[index]["ID"])         elif choise.upper() == "Q":  # 退出时 不计算商品             print("退出购物商城,无购物".center(15, "*"))             man()         elif choise == "n":  # 结算 商品             while 1:                 for i in range(len(shop_car)):  # 打印购物车内商品信息                     cost_money += shop_car[i]["price"] * shop_car[i]["totle"]                 while 1:  # 进入移除商品循环                     if len(shop_car) == 0:                         print("购物车内无商品")                         print("购物车已清空","\n无消费,余%s元" % (int(salary) - int(cost_money)))                         man()                     if int(salary) > int(cost_money):  # 购物金额充足时                         print("您购买商品信息如下".center(20, "*"), "\n商品名称", "数量".center(10, " "), "价格")                         for i in range(len(shop_car)):                             print(shop_car[i]["name"], str(shop_car[i]["totle"]).center(12, " "), shop_car[i]["price"])                         print("共消费%s元,余%s元" % (int(cost_money), int(salary) - int(cost_money)))                         exit()                     else:  # 购物金额不足时                         chajia = int(salary) - int(cost_money)  # 差的钱数                         print("购物金额不足,请移除部分商品,还差%s元" % (chajia))                         print("ID", "商品名称".center(5, " "), "数量".center(7, " "), "价格")                         for i in range(len(shop_car)):  # 打印购物车内要移除的商品列表                             print(shop_car[i]["ID"], shop_car[i]["name"].center(7, " "),                                   str(shop_car[i]["totle"]).center(9, " "), shop_car[i]["price"])  # 输出商品列表信息                         while 1:                             choise = input("输入移除商品ID,退出商城请按[Q]:>>")  # 选择移除商品 ID                             if choise.isdigit():                                 choise = int(choise)                                 if choise >= 0 and choise in ID_lst:  # 存在一个小BUG                                     break                                 else:                                     print("输入ID有误,请重新输入")                                     continue                             elif choise.upper() == "Q":                                 man()                             else:                                 print("输入ID有误,请重新输入")                         for item in shop_car:                             if item["ID"] == choise and item["totle"] == 1:                                 cost_money -= item["price"]  # 商品为1件时移除 花费钱还回来                                 shop_car.remove(item)                                 ID_lst.remove(int(item["ID"]))                             elif item["ID"] == choise:  # 商品数量 减少                                 cost_money -= item["price"]                                 item["totle"] -= 1                                 ID_lst.remove(int(item["ID"]))             exit()         else:             print("输入有误,请重新输入:>>") def quit():     print("已退出系统".center(15, "*"))     exit() man()

转载于:https://www.cnblogs.com/pengjihao/p/10737109.html

你可能感兴趣的文章
BZOJ1026: [SCOI2009]windy数
查看>>
bash 学习笔记3
查看>>
青岛Uber优步司机奖励政策(12月28日到1月3日)
查看>>
js时间加减
查看>>
夏天能让"蚊子"、蟑螂绝子绝孙的秘诀。
查看>>
Java for LeetCode 132 Palindrome Partitioning II
查看>>
Java review-basic1
查看>>
android TextView Input 实例
查看>>
今夜,很思念我的妻儿
查看>>
Matrix (二分套二分
查看>>
WY c语言 (7)
查看>>
洛谷P1197 星球大战【并查集】
查看>>
python base64 编解码,转换成Opencv,PIL.Image图片格式
查看>>
我的自白 —— 未来的期待
查看>>
解决sql sever2000 远程连接失败 error40 问题
查看>>
Android开发学习总结——搭建最新版本的Android开发环境
查看>>
pat L2-019. 悄悄关注
查看>>
挑战程序设计2 矩阵链乘
查看>>
将博客园或CSDN任意博主的全部博文备份到github上的简单方法
查看>>
go语言学习 一
查看>>