简易购物车之一

# 商品信息:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
# 用户信息:
user = {"username":"alex", "password": "123456"}


# 功能要求:
# 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
# 2、用户根据商品编号购买商品
# 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
# 4、退出时,打印已购买商品和余额

# 准备一个购物车
# 购物车:[{id:编号, name:名称, price:商品价格, totle:数量}]
# [{id:0, name:"电脑", price:1999, totle:7},
# {id:3, name:"美女", price:998, totle:2}]

# 答案:
# shoppingcart = []
#
# while 1:
# uname = input("请输入你的用户名:")
# upwd = input("请输入你的密码:")
# if uname == user['username'] and upwd == user['password']:
# print("登录成功!")
# money = int(input("请输入你兜里钱:"))
#
# while 1:
# for i in range(len(goods)):
# print(i+1, goods[i]['name'], goods[i]['price'])
# # 1 键盘 100 0
# # 2 鼠标 200 1
# # 3 美女 1000 2
# # 2
# num = int(input("请输入你要购买的商品编号:")) # 我没判断是否是数字
# # 还原回索引
# index = num - 1
# # 获取到购买的商品
# good = goods[index]
# # 判断是否可以购买该商品
# if money >= good['price']:
# # 判断是否已经购买过该商品, 如果购买过. 数量+1 没买过 加到购物车里
# for el in shoppingcart: # el:你已经够买过的商品
# if el['id'] == index: # 买过
# el['totle'] += 1 # 数量+1
# break # 继续显示商品列表
# else: # 没买过
# shoppingcart.append({"id": index, "name": good['name'], "price": good['price'], "totle": 1})
# money -= good['price'] # 扣钱
# print("购买成功!!, 您的余额是%s" % money)
# else:
# print("对不起. 您的余额已不足!, 您的余额还剩%s" % money) # 可以充值也可以不充值
#
# # 是否继续购物
# isContinue = input("请问是否继续购买商品(Y/N)")
# if isContinue.upper() == "N":
# # 不买了
# # 打印购买的商品和余额
# for g in shoppingcart:
# print(g['name'], g['price'], g['totle'])
# print("你还剩下%s" % money)
# # 程序退出
# exit() # 程序退出
# else:
# print("登录失败!")





posted @ 2019-01-02 15:41  威霸  阅读(151)  评论(0编辑  收藏  举报