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("输入商品编号或者退出")




  • 相关阅读:
    Java学习笔记(三)——运算符
    [镜像]镜像操作losetup
    [DNS]部署局域网DNS服务器
    [3G/4G]3G/4G模块开发
    [4G]常用AT指令
    [ubuntu]E: The package firmware-upgrade needs to be reinstalled, but I can't find an archive for it.
    [mmc]Linux下MMC/SD/SDIO的识别与操作
    [MMC]Linux MMC/SD/SDIO驱动分析
    [mmc]设备树节点含义
    [uboot]What is MLO file?
  • 原文地址:https://www.cnblogs.com/chenjiao0904/p/9689693.html
Copyright © 2011-2022 走看看