购物车

goods = [{"name": "电脑", "price": 1999}
         {"name": "鼠标", "price": 10},
         {"name": "游艇", "price": 20},
         {"name": "美女", "price": 998}
         ]
dic={}
l1=[]#购买的商品
l2=[]#商品数量
sum=0
for i in goods:
    print(goods.index(i)+1, i["name"],i["price"])
flg = True
while flg:
    money = input("请输入资产")
    if money.upper() == "Q":
        flg = False
        continue
    elif money.isdigit() and int(money)>=0:
        flg1 = True
        while flg1:
            a = input("选择商品序号,或q退出或p返回上层")
            if a.upper()=="Q":
                flg = False
                flg1 = False
                break
            elif a.upper()=="P":
                flg = True
                break
            elif  a.isdigit() and int(a) in range(1,len(goods)+1):
                l1.append(a)
                flg2 = True
                while flg2:
                    b = input("选择购买个数,或退出或返回上一层")
                    if b.upper()=="Q":
                        flg = False
                        flg1 = False
                        break
                    elif b.upper()=="P":
                        break
                    elif b.isdigit() and int(b)>=0:
                        l2.append(b)
                        flg3 = True
                        while flg3:
                            d = input("继续购买y或结账n")
                            if d.upper()=="Y":
                                flg2 = False
                                break
                            elif d.upper()=="N":
                                for f in  range(len(l1)):
                                    money1=goods[int(l1[f])-1]["price"]*int(l2[f])
                                    sum+=money1
                                if int(money)>=sum:
                                        print("购买成功")
                                        print("花了%s,余额为%s" %(sum,int(money)-sum))
                                        for f in range(len(l1)):
                                            print("购买了%s个%s" %(l2[f],goods[int(l1[f])-1]["name"]))
                                        flg=False
                                        flg1=False
                                        flg2 = False
                                        flg3=False
                                        break
                                else:
                                    flg4=True
                                    while flg4:
                                        print("余额不足,是否充值Y/N")
                                        cho = input("--->")
                                        if cho.upper()=="Y":
                                            flag = True
                                            while flag:
                                                k = input("充值金额")
                                                if k.isdigit() and int(k)>0:
                                                    money = int(money)+int(k)
                                                    flag=False
                                                    flg4=False
                                                    break
                                                else:
                                                    print("重新输入")
                                                    continue
                                        elif cho.upper()=="N":
                                            print("欢迎再来")
                                            flg = False
                                            flg1 = False
                                            flg2 = False
                                            flg3=False
                                            flg4 = False
                                            break
                                        else:
                                            print("重新输入")
                                            continue

                            else:
                                print("输入错误重新选择")
                                continue


                    else:
                        print("输入错误,重新输入")
                        flg = False
                        flg1=False
                        continue

            else:
                print("商品序号不对重新输入")
                flg = False
                continue
    else:
        print("输入不正确,重新输入")

 

posted @ 2018-03-06 17:54  wzqwer  阅读(130)  评论(0编辑  收藏  举报