zoukankan      html  css  js  c++  java
  • 照葫芦画瓢之老男孩购物车程序

    salary = input('welcome to shopping,please input you salary:')           #请输入你的工资
    product_list = [('iphone--->', 5999),                                    #产品列表
             ('watch', 599),
             ('tcl', 799),
             ('book', 30),
             ('paper', 15),
             ('pen', 100)
             ]
    shopoing_list = []
    if salary.isdigit():                                                     # 如果输入的工资为数字,执行if语句
        salary = int(salary)                                                 # 如果这里直接强转int,输入的不是整数就会报错
        while True:
            for index, i in enumerate(product_list):                         # 通过enumerate方法取出product_list里的索引
                print(index, i)
            user_chioce = input('请选择要买的商品序号:')
            if user_chioce.isdigit():
                user_chioce = int(user_chioce)
                if user_chioce < len(product_list) and user_chioce >= 0:     #用户输入的数字在可选的范围之内(0-5)
                    p_item = product_list[user_chioce]                       #取出用户选择的商品价格
                    if p_item[1] <= salary:
                        shopoing_list.append(p_item)
                        salary -= p_item[1]                                  #将已经用掉的钱从salary中扣除
                        print('Added %s into shopping cart,your current balance is %s'%(p_item,salary))
                    else:
                        print(' balance is insufficient')
                else:
                    print('Product does not exist')
            elif user_chioce == 'q':
                print('----------SHOPPING LIST----------')
                for p in shopoing_list:
                    print(p)
                print('current balance:%s'%salary)
                break
            else:
                print('input error!please enter again!thanks')
    else:
        print('salary error!')






    """
        a = [1,2,3]
        for i in enumerate(a):                                enumerate()方法测试程序
            print(i)
    """
    """     for i in product_list:
                print(product_list.index(i),i)                通过index取出product_list中每个元素的索引即0 1 2 3 4 5
            break
    """

  • 相关阅读:
    JqueryQrcode生成二维码不支持中文的解决办法
    [转载]浅析海量用户的分布式系统设计
    [转载]大型网站应用中 MySQL 的架构演变史
    CSS3变形记(上):千变万化的Div
    JavaScript进阶之路——认识和使用Promise,重构你的Js代码
    Visual Studio Code预览版Ver 0.3.0试用体验
    Apache Spark 2.3.0 正式发布
    Apache Spark 2.2.0 新特性详细介绍
    Apache Spark 2.2.0 正式发布
    Spark 论文篇-论文中英语单词集
  • 原文地址:https://www.cnblogs.com/MisterZZL/p/9534304.html
Copyright © 2011-2022 走看看