zoukankan      html  css  js  c++  java
  • Week2-购物车程序

    #!/usr/bin/en python
    # Author:lijun
    
    '''
    程序:购物车程序
    
    需求:
    
    启动程序后,让用户输入工资,然后打印商品列表
    允许用户根据商品编号购买商品
    用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
    可随时退出,退出时,打印已购买商品和余额
    '''
    
    goods_list = [["iphone",6000],["macbook",12000],["book",88],["bike",900]]
    salary = input("请输入您的月工资:")
    
    shopping_car=[]
    if salary.isdigit():
        salary = int(salary)
        while True:
            for index,item in enumerate(goods_list):
                print("商品编号:%d,商品名称:%s,商品价格:%d" %(index,item[0],item[1]))
    
            want_buy = input("请输入您要购买的商品编号:")
    
            if want_buy.isdigit():
                want_buy = int(want_buy)
                if want_buy < len(goods_list) and want_buy>=0:
                    price=goods_list[want_buy][1]
                    if price <= salary:
                        shopping_car.append(goods_list[want_buy])
                        salary-=price
                        print("您购买了商品:%s,余额:%s" %(goods_list[want_buy][0],salary))
                    else:
                        print("您的余额为%s,买不起%s" %(salary,goods_list[want_buy][0]))
                else:
                    print("输入错误,请输入正确的商品编号和商品名称")
            elif want_buy == "q":
                if shopping_car == []:
                    print("退出成功,您的购物车为空,您的余额为:%d" %salary)
                else:
                    print("您购买了以下商品:")
                    for i in shopping_car:
                        print(i[0])
                    print("您的余额为:%s" %salary)
                break
            else:
                print("输入错误.")
  • 相关阅读:
    linux 查看磁盘空间
    nginx面试中最常见的18道题
    spring -mvc service层调用工具类配置
    IntelliJ IDEA tomcat 远程Ddbug调试
    IntelliJ IDEA tomcat 热部署
    Java HttpClient PostMethod
    Java Base64 加密/解密
    启动tomcat时cmd窗口一闪而过
    remote staging type or host is not specified
    Maven更新后本地仓库jar后缀带有 lastUpdated
  • 原文地址:https://www.cnblogs.com/pythonlee/p/9548694.html
Copyright © 2011-2022 走看看