• 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")
  • 相关阅读:
    asp.net页面生命周期追踪
    asp.net Forums 之配置,缓存,多数据访问
    沪江技术部程序员招聘试题,大家一起讨论一下。
    httpd does not appear to be running and proxying cobbler, or SELinux is in the way.
    网络知识OSI七层网络与TCP/IP五层网络架构及二层/三层网络
    python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件
    Linux中访问Apache报403错误处理方法
    centos7的启动流程
    pycharm介绍
    监测NGINX服务的shell脚本
  • 原文地址:https://www.cnblogs.com/Kate-liu/p/9903980.html
Copyright © 2020-2023  润新知