zoukankan      html  css  js  c++  java
  • 优化(购物项目)

    用for循环输出商品名单,其中用了enumerate函数对列表进行格式化输出

    #_author:Administrator
    #date:2019/10/25
    shopping_list=[
    ('iphone11',5800),
    ('mac book ',9000),
    ('coffee ',38),
    ('python book',80),
    ('bicycle',1200),
    ]
    saving=input("please input your salary:")
    shopping_car=[]

    if saving.isdigit():
    saving=int(saving)
    while True:
    #for循环用来打印商品内容
    for i,v in enumerate(shopping_list,1):#enumerate()函数自动为每一项添加序号,从0开始,因此步长设置为1

    print(i,'----->',v)#用两个变量去接收元组,i和v之间可以加描述符
    #引导用户进行输入
    chose = input("选择购买商品编号[退出:q]")
    #验证输入是否合法
    if chose.isdigit():
    chose=int(chose)
    if chose>0 and chose<=len(shopping_list):
    #将用户选择商品通过chose取出来
    sh_item=shopping_list[chose-1]
    #如果钱够,用本金减去商品价格,并将该商品加入购物车
    if sh_item[1]<saving:
    saving-=sh_item[1]
    shopping_car.append(sh_item)

    else:
    print('余额不足,你的余额还有%d'%saving)
    print(sh_item)

    else:
    print('编码不存在')
    elif chose == "q":
    print('---------------------你已经购买如下商品---------------------')
    #循环遍历购物车里的商品,购物车里面存放的是已买商品
    for i in shopping_car:
    print(i)
    print('你还剩%d块钱'%saving)
    break

    else:
    print("invalid input")
    Output:

    please input your salary:1
    1 -----> ('iphone11', 5800)
    2 -----> ('mac book ', 9000)
    3 -----> ('coffee ', 38)
    4 -----> ('python book', 80)
    5 -----> ('bicycle', 1200)
    选择购买商品编号[退出:q]q
    ---------------------你已经购买如下商品---------------------
    你还剩1块钱




  • 相关阅读:
    火狐URL编码问题
    Asp.net动态关键字
    charindex ,PATINDEX,contains,FREETEXT用法
    PanGu词库批量添加关键词
    Dictionary SortedList HashSet List用法
    hubbledotnet查询速度慢的问题
    asp.net 页面static变量问题
    String.Concat和String.Format用法
    .net显示今天农历的代码
    存储过程用户登录
  • 原文地址:https://www.cnblogs.com/startl/p/11740364.html
Copyright © 2011-2022 走看看