购物车作业
user = [{"name": "电脑", "password": 1999,"salay": 1999},{"name": "鼠标", "password": 10,"salay": 1999},{"name": "游艇", "password": 20,"salay": 1999},{"name": "美女", "password": 998,"salay": 1999},] goods = [{"name": "电脑","price": 1999},{"name": "鼠标", "price": 10},{"name": "游艇", "price": 20},{"name": "美女", "price": 998},] shopping_cart = [] exit_flag = False while not exit_flag: user_name = input(" 请输入用户名: ")#输入信息 Flag = False pass_flag = False for item in user: if user_name == item["name"]: Flag = True user_password = input(" 请输入密码: ") if user_password == item["password"]: pass_flag = True print('登录成功!') print("----商品列表----") else: print('''密码错误,请重新输入密码''') user_password = input("请输入输入密码:") continue else: password = [] login_name = [] salay = [] login_name = input("请注册用户名") salay = input("请输入您的工资:") #注册 d1 = dict(zip(password , login_name,salay)) user.append(d1) print('''注册成功,%s你好,您当前余额为:%s''' % (user_name, salay)) break for k, i in enumerate(goods): print('%s. %s price:%s' % (k, i['name'], i['price']))#接下来是用工资和商品价格 choice = input("输入想买的商品编号:") if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(goods): if salay >= choice: shopping_cart.append(goods[choice]) print('added goods %s into shopping_cart.'%(goods[choice]['name'])) salay -= goods[choice]['price'] else: print("余额不足") elif choice == 'q': if len(shopping_cart) > 0: print("----你已经购买了----") for k, i in enumerate(goods): print('%s. %s price:%s' % (k, i['name'], i['price'])) print("余额::",salay) exit_flag = True