zoukankan      html  css  js  c++  java
  • 写一个循环,不断的问客户想买什么 ,当用户选择一个商品编号,就把对应的商品加入购物车 ,最终用户输入q退出时,答应购物车的商品

    #写一个循环,不断的问客户想买什么 ,当用户选择一个商品编号,就把对应的商品加入购物车 ,最终用户输入q退出时,答应购物车的商品
    products=[["Iphone8",68888],["MacpPro",14800],["Coffee",31],["小米",2499],["Book",80],["Nlke",799]]
    shopping_cart=[]
    print(".....商品信息......")
    #while True:
    run_flag=True
    #exit_flag=False
    #while not exit_flag:
    while run_flag: #条件一直是真
    for index,i in enumerate(products): #enumerate 是枚举
    print("%s. %s %s"%(index,i[0],i[1])) #占位符格式化输出
    choices=input("请输入你想购买的商品编号:") # 输入的值赋值给一个变量
    if choices.isdigit(): #isdigit是判断是数字 如果输入的数字 则choices强制转换为int
    # print(len(products)) #len() 取列表的长度
    choices=int(choices)
    if choices>0 and choices<len(products): #choices的长度 要小于长度
    shopping_cart.append(products[choices]) #把输入的商品编号对应商品存到一个列表
    print("商品%s已加入购物车"%(products[choices])) #打印商品加入购入车
    else:
    print("输入的商品存在 ")
    elif choices=='q':
    if len(shopping_cart)>0:
    print("......购物车的商品........")
    for index,i in enumerate(shopping_cart):
    print("%s. %s %s "%(index,i[0],i[1]))
    else:
    print("还没有购买商品")
    run_flag=False
    # exit_flag=True

    else:
    print("输入商品编号或者退出")




  • 相关阅读:
    一个简单粗暴的爬虫
    Linux 目录结构
    python 部署 Restful web
    JVM 运行时数据区总结 栈 堆 堆大小配置总结
    成都法律援助申请流程
    JavaEE error整理(不断更新)
    ehcache.xml 属性大全
    SpringMVC 构建Restful风格 及问题处理
    Http Content-Type
    Redis 教程 Java工程师学习知识点
  • 原文地址:https://www.cnblogs.com/chenjiao0904/p/9689693.html
Copyright © 2011-2022 走看看