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")
  • 相关阅读:
    CCF CSP 201509-1 数列分段
    CCF CSP 201503-1 图像旋转 (降维)
    CCF CSP 201412-1 门禁系统
    CCF CSP 201409-1 相邻数对
    CCF CSP 201403-1 相反数
    CCF CSP 201312-1 出现次数最多的数
    Win10环境下 HTTP 错误 500.19
    牛客网 整数拆分 (动态规划)
    牛客网 放苹果
    LeetCode9 回文数
  • 原文地址:https://www.cnblogs.com/Kate-liu/p/9903980.html
Copyright © 2011-2022 走看看