• 列表练习题 简单购物车


    个人代码,今后修改

    products = [['Iphone',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
    
    #打印商品列表
    print('--------------商品列表-----------')
    for index,i in enumerate(products):
        print(index,i[0],i[1])
    #购物车
    shopping_list = [["占位",1,1]]
    run_flag = True
    while run_flag:
        id = input("请输入你要购买的商品(按q退出):")
        if id =='q' or id == 'Q':
            if len(shopping_list) > 1:
                print("结束服务!您购买的商品如下!")
                for i in shopping_list[1:]:
                    print('商品名:',i[0],' 价格:',i[1],' 数量:',i[2])
            print("感谢购物 再见!")
            run_flag = False
        elif id.isdigit():
            id = int(id)
            if id <0 or id > len(products):
                print("您输入的商品号不正确,请重新输入!")
                continue
            else:
                print("您选择了商品是:",products[id][0],",价格是:",products[id][1],',已成功添加进购物车!')
                #查询商品是否存在购物车中
                x = 0
                for i in shopping_list:
                    if products[id][0] in i[0]:
                        x = 1
                        i[2] += 1
                        break
                    else:
                        x = 0
                if x == 0:
                    shopping_list.append([products[id][0], products[id][1], 1])
        else:
            print("请输入正确的商品号!")
            continue
  • 相关阅读:
    简历的快速复制
    使用stringstream对象简化类型转换
    猴子吃桃
    new和delete运算符
    绘制正余弦曲线
    计算学生的平均成绩
    判断是否为回文字符串
    统计各种字符个数
    验证用户名
    回溯法(挑战编程)
  • 原文地址:https://www.cnblogs.com/alexstraze/p/9241274.html
Copyright © 2020-2023  润新知