zoukankan      html  css  js  c++  java
  • python基础--购物车程序

      购物车例子,实现显示商品信息,输入商品编号并且可以减去自己的存入余额,当商品价格大于自己的余额的时候,直接退出;当不再选择商品的时候,退出显示余额和已经添加的商品。

    #购物车程序
    
    product_list = [
        ("airplane",90000),
        ("pen", 80),
        ("Trek bike", 5000),
        ("Book", 200),
        ("salt", 10),
        ("clothes", 600),
    
    ]
    
    saving = input("please input your money:")  #存入的本金
    shopping_car = []
    
    if saving.isdigit():      #判断存入的是不是数字
        saving = int(saving)
        while True:
            for i,v in enumerate(product_list,1):
                print(i,">>>>",v)
            choice = input("选择购买商品编号[退出:q]:")
    
            if choice.isdigit():
                choice = int(choice)
                if choice > 0 and choice <= len(product_list):
                    p_item = product_list[choice - 1]
                    if p_item[1] < saving:
                        saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
                        shopping_car.append(p_item) #购物的信息
                    else:
                        print("余额不足,还剩%s,不能购买下面的商品"%saving)
                        print(p_item)
                else:
                    print("您输入的编号不存在")
            elif choice == "q":
                print("--------您购买的商品如下--------")
                for i in shopping_car:
                    print(i)
                print("您还剩%s元钱"%saving)
                break
            else:
                print("invalid input")
    #购物车程序
    
    product_list = [
        ("airplane",90000),
        ("pen", 80),
        ("Trek bike", 5000),
        ("Book", 200),
        ("salt", 10),
        ("clothes", 600),
    
    ]
    
    saving = input("please input your money:")  #存入的本金
    shopping_car = []
    
    if saving.isdigit():      #判断存入的是不是数字
        saving = int(saving)
        while True:
            for i,v in enumerate(product_list,1):
                print(i,">>>>",v)
            choice = input("选择购买商品编号[退出:q]:")
    
            if choice.isdigit():
                choice = int(choice)
                if choice > 0 and choice <= len(product_list):
                    p_item = product_list[choice - 1]
                    if p_item[1] < saving:
                        saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
                        shopping_car.append(p_item) #购物的信息
                    else:
                        print("余额不足,还剩%s,不能购买下面的商品"%saving)
                        print(p_item)
                else:
                    print("您输入的编号不存在")
            elif choice == "q":
                print("--------您购买的商品如下--------")
                for i in shopping_car:
                    print(i)
                print("您还剩%s元钱"%saving)
                break
            else:
                print("invalid input")
  • 相关阅读:
    JS实现——用3L和5L量出4L的水
    岭回归与Lasso回归
    简单多元线性回归(梯度下降算法与矩阵法)
    【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
    tensorflow sigmoid_cross_entropy_with_logits 函数解释
    tensorflow 线性回归解决 iris 2分类
    神经网络激活函数
    神经网络防止过拟合的方法
    网络流量分析——NPMD关注IT运维、识别宕机和运行不佳进行性能优化。智能化分析是关键-主动发现业务运行异常。科来做APT相关的安全分析
    cnn汉字识别 tensorflow demo
  • 原文地址:https://www.cnblogs.com/Kate-liu/p/9903980.html
Copyright © 2011-2022 走看看