Python 简易购物系统--老男孩作业

from colorama import init,Fore
import os,time
init(autoreset=True)

user_list = {"Gary":["password",8000],"Larry":["password",10000],"Peter":["password",20000]}
shops_goods = {"0001":["iPhone ",4800,10],"0002":["Ipad   ",2200,8],"0003":["MacBook",8000,5],"0004":["Itouch ",1200,20]}

shopping_cart = {}  # 购物车

def cart_deal(good_name,counts,list=shopping_cart):
    if good_name not in list:
        list[good_name] = counts
    else:
        list[good_name] += counts

def list_goods():
    for v in shops_goods:
#        return v,shops_goods[v]
        print(v,shops_goods[v])
def add_goods(snum,counts):
    if snum in shops_goods:
        shops_goods[snum][2] += counts

while 1:
    shop_ch = input("购物请按1,回车;后台管理请按2,回车:")
    if shop_ch == "2":
        num_choose = input("Plse choose Your:\
        \n---->1. 添加货物\
        \n---->2. 更改价格\
        \n---->3. 产品下架\
        \n 请选择:")
        if num_choose == "1":
            list_goods()
            snum_input = input("请输入您要添加产品的编号:")
            if snum_input not in shops_goods.keys():
                new_produ = input("请输入产品名称:")
                new_price = input("请输入产品价格")
                new_counts = input("请输入上架数量")
                shops_goods[snum_input] = [new_produ,int(new_price),int(new_counts)]
            else:
                add_produ_counts = input("请输入上架数量:")
                shops_goods[snum_input][2] += int(add_produ_counts)        
            list_goods()

        if num_choose == "2":
            list_goods()
            snum_input = input("请输入您要修改价格产品的编号:")
            print(shops_goods[snum_input][0],shops_goods[snum_input][1])
            new_value = input("请输入产品最新价格:")
            shops_goods[snum_input][1] = new_value
            list_goods()
            
    if shop_ch == "1":
        while 1:
            try:
                user_login = input("\n请输入登录用户名(输入r返回):")
                if user_login == "r":
                    break
                user_pass = input("请输入密码:")
                if user_list[user_login][0] == user_pass:
                    os.system("cls")
                    print(Fore.GREEN + "-------系统登录中,请稍后.....-------")
                    time.sleep(3)
                    os.system("cls\n")
                    print(Fore.YELLOW + "---->>欢迎回来<<----,Mr(Miss) %s\n" % user_login)
                    while 1:
                        print("您当前账户余额 " + Fore.RED+ "%d ¥" % user_list[user_login][1])
                        print("----------------------------------------")
                        print("编号    产品     价格(RMB)   库存")
                        for g in shops_goods:
                            print("%s     %s   %d          %s" % (g,shops_goods[g][0],shops_goods[g][1],shops_goods[g][2]))
                        print("----------------------------------------")
                        num_buy = input("请输入要购买产品的编号(充值请输入:cz  退出:q):")
                        if num_buy == "cz":
                            cz_value = input("请输入充值金额:")
                            if cz_value.isdigit:
                                user_list[user_login][1] += int(cz_value)
                            continue
                        if num_buy == "q":
                            break
                        if num_buy in shops_goods:
                            num_counts = input("请输入购买的数量:")
                            if int(num_counts) > shops_goods[num_buy][2]:
                                print("库存不够,请重新选择!")
                                continue
                            else:
                                if user_list[user_login][1] < shops_goods[num_buy][1] * int(num_counts):
                                    print("余额不足。请充值!\n")
                                    time.sleep(1)
                                    os.system("cls")
                                    continue
                                shops_goods[num_buy][2] -= int(num_counts)
                                cart_deal(shops_goods[num_buy][0],int(num_counts))
                                user_list[user_login][1] -= shops_goods[num_buy][1] * int(num_counts)
                            os.system("cls")
                            print("\n>>>>>>>>购物车:\n")
                            for i in shopping_cart:
                                print(i,shopping_cart[i])
                            print("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
                        else:
                            print(Fore.WHITE+"您选择的产品不存在")
            except KeyError:
                print(Fore.GREEN + "用户名或者密码错误\n")
                continue
            except KeyboardInterrupt:
                print(Fore.RED+"\n--->警告!输入正确选项<---")
                continue
        

 

posted @ 2016-07-29 15:43  loyfee  阅读(1544)  评论(0编辑  收藏  举报