基于python实现购物车功能

意义

利用购物车系统可以熟练掌握,pyhon入门操作,如 while 循环 if语句,字符串方法,插入,删除数据,


业务流程

       购物车程序

界面:

            salary = 5000
        1.  iphone6s  5800
        2.  mac book    9000
        3.  coffee      32
        4.  python book    80
        5.  bicyle         1500
      >>>:1
         余额不足,-3000     #选择商品后计算余额
      >>>:5
      已加入bicyle 到你的购物车, 当前余额:3500
      >>>:quit
      您已购买一下商品
      bicyle    1500
      coffee    30
      您的余额为:2970

      欢迎下次光临

实现

#___author  Laughing
#date
id=[1,2,3,4,5]     #初始换五个商品
shop=['car','phone','coffe','water','soccer'] #初始化商品列表
money=[500000,5800,32,3,50]      #初始化商品金额
shop_choose=['nothing']          #若在没有购物的情况选择退出,我们打印nothing
shopping_list='''                    
-----------------shopping-list---------------------
        %d:%s-------------------%d
        %d:%s-----------------%d
        %d:%s-----------------%d
        %d:%s-----------------%d
        %d:%s----------------%d
        0----------------------quit
----------------------------------------------------
'''% (id[0],shop[0],money[0],id[1],shop[1],money[1],id[2],shop[2],money[2],id[3],shop[3],money[3],id[4],shop[4],money[4])
print("plases input your salary :   ")
salary=int(input())
Balance=salary
i=0
while True:
    print("plases input the shop of id what you want")
    print(shopping_list)
    num=int(input())
    if num == 0:                #判断退出
        print('''
        -------------------shopping cart------------------
        your choose:     %s
        Balance:         %d
        welcome to here!
        -----------------------End--------------------------
        '''% (shop_choose,Balance))
        exit()
    if i == 0:
        del shop_choose[0]
        i =i+1

    shop_choose.append(shop[num-1])
    print(shop_choose)
    Balance -= money[num-1]
    print('''
            -------------------shopping cart------------------
            your choose:     %s
            Balance:         %d
            welcome to here!
            -----------------------End--------------------------
            ''' % (shop_choose, Balance))

优化(重点)

但以上代码,弊端太大,不支持滚键盘,选择内容改变太难(一个列表改变另一个也要改变)为了减少工作量,可以进行优化

讲物品和钱放在同一列表中,添加判实现滚键盘。

#___author  Laughing
#date
shop=[('car',500000),('phone',5800),('coffe',32),('water',3),('soccer',3)]
shop_choose=[]
while True:
    print("plases input your salary :   ")
    salary=input()
    if salary.isdigit():
        salary=int(salary)
        break
    else:
        print("wrong ,plases input digit")
Balance=salary
while True:
    for i,j in enumerate(shop,1):
        print(i,j)
    print("plases input the shop of id what you want")
    num=input()
    if num.isdigit():
        num=int(num)
        if  num <= 0 or num > len(shop):
            print("plaese input right num")
            continue
    elif num =='q' or num == 'Q':
        print(shop_choose)
        print(Balance)
        print("bye ")
        break
    else:
        print("please input digit")
        continue
    shop_choose.append(shop[num-1][0])
    Balance -= shop[num-1][1]
    print(shop_choose)
    print(Balance)





发布了20 篇原创文章 · 获赞 0 · 访问量 3812

猜你喜欢

转载自blog.csdn.net/Laughing_G/article/details/80631585