简单的电商购物程序1
def logon(tename, tepassword, credit): if tename == logname and tepassword == logpassword: print("------------------------") print("欢迎您 " + name + " 使用翰海购买系统") print("您的积分为:" + str(credit) + " 等级为:" + Membertype(credit)) else: print("账号密码错误请重新输入 或 注册账号") menbertypr = "倔强青铜" def Membertype(credit): if credit < 100: menbertypr = "倔强青铜" elif 100 <= credit < 200: menbertypr = "秩序白银" elif 200 <= credit < 300: menbertypr = "荣耀黄金" elif 300 <= credit < 400: menbertypr = "尊贵铂金" else: menbertypr = "永恒钻石" return menbertypr cre = 0 def like(): i = 2 # 为了通过修改i 退出多重循环 allChoice = [] while (i): for num in range(len(shop)): # 打印商品列表 print(str(shop[num][0]).ljust(5), shop[num][1].ljust(20), str(shop[num][2]).ljust(10), str(shop[num][3]).ljust(10)) choice = input("请输入要加入购物车的商品编号:") choice = [int(it) for it in choice.split(' ')] allChoice += choice # choice是单次选择的商品列表,allchoice是所有选择的商品列表 while (1): total = 0 total1 = 0 global cre credit = cre choiceSet = set(allChoice) # 转换成集合,便于不重复计数 for it in choiceSet: print(shop[it - 1][0], shop[it - 1][1], shop[it - 1][2], '*', allChoice.count(it)) total += shop[it - 1][2] * allChoice.count(it) print("总计:", total, ) print("---------------------------------\n" "1:继续选购 2:整理购物车 Buy:结算\n") option = input("请选择:") if option == '1': break elif option == '2': item_num = int(input("请输入要删除的商品:")) allChoice.remove(item_num) # 每次只会删除一个元素 continue if option == 'Buy': for it in choiceSet: print("*************结算**************") print(shop[it - 1][0], shop[it - 1][1], shop[it - 1][2], '*', allChoice.count(it)) total1 += shop[it - 1][2] * allChoice.count(it) credit += shop[it - 1][3] * allChoice.count(it) print("总计:", total1, "积分为:", credit, "会员等级:", Membertype(credit)) print("-----信息更新成功!!!------") print("尊敬的", name, "性别", sex) print("您的积分:" + str(credit) + " 等级为:" + Membertype(credit)) print("-----感谢您的使用,再见!------") cre = credit i = 0 break else: print("输入错误请重新输入!") shop = {} a = 1 shop = [['1', '吉利', 100000, 100], ['2', '比亚迪', 80000, 100], ['3', '奇瑞', 68000, 100], ['4', '长城', 120000, 100],['5', '哈弗', 100000, 50], ['6', '奥迪', 230000, 100], ['7', '红旗', 100000, 100]] while (a): a = input("请输入“登录”或“注册”:") if a == "注册": name = input("请输入姓名:") sex = input("请输入性别:") logname = input("请输入账号:") logpassword = input("请输入密码:") credit = cre = 0 elif a == "登录": tename = input("请输入账号:") tepassword = input("请输入密码:") try: credit = cre logon(tename, tepassword, credit) like() except: print("请先注册帐号!")