• 购物车(list)


    product_list=[#建列表
        ('Iphone',5800),
        ('MacPro',9800),
        ('Bike',800),
        ('Watch',10600),
        ('Coffee',31),
        ('Alex python',120),
    ]
    shopping_list=[]#购物车列表为空
    salary=input("Input your salary:")#输入工资(工资只输入一次,所以放在循环外。且因输入默认为字符串不能强转,否则报错)
    if salary.isdigit():#判断所输入的salary是否是数字,返回1/2
        salary=int(salary)#若为真则强转为int型
        while True:
         #   for item in product_list:#循环打印商品列表
                for index,item in enumerate(product_list):#循环打印商品列表及下标,并取出下标
                    '''>>>a=[1,2,3]
                      >>>for i in enumerate(a):
                      >>>print(i)
                     (0,1)
                     (1,2)
                     (3,3)
                     '''
                    print(index,item)#打印商品列表
                user_choice=input("选择要买:")#用户选择输入的商品
                if user_choice.isdigit():#如果输入的是(字符型的)数字
                    user_choice=int(user_choice)#转int
                    if user_choice<len(product_list)and user_choice>=0:#所输入的数字是否<列表长and >=0
                        p_item=product_list[user_choice]#通过下标获取商品价格
                        if p_item[1]<=salary:#判断价格是否<=工资
                            shopping_list.append(p_item)#若买得起就(添加)存入购物车列表
                            salary-=p_item[1]#工资-商品
                            print("Added %s into shopping cart ,your current banlance is 33[31;1m %s33[0m" %(p_item, salary))
                        else:
                           print("33[41;1m你的余额为[%s]33[0m" %salary)#若买不起则打印...
                    else:
                       print("product code[%s]is not exits!" %user_choice)#若所输入不标准则打印商品不存在
                elif   user_choice=="q":#若输入的是q,则打印所买物品,退出
                    print("-------shopping list------")#打印以输出的
                    for p in shopping_list:
                        print(p)
                    print("you current balamce:",salary)#打印所剩余额
                    exit()#退出
                else: #若不是q
                    print("invalid optin")#打印错误选项
    
    

    0102

  • 相关阅读:
    uoj388 【UNR #3】配对树
    uoj386 【UNR #3】鸽子固定器
    回忆录
    xcode 把项目代码提交到远程SVN服务器
    IOS 点击按钮拨号
    ADO与达梦7产生的一个未知问题
    DSN 建立达梦7(DM)连接
    iOS UIControl 事件的说明(转)
    IOS开发copy,nonatomic, retain,weak,strong用法
    QT Creator 使用SVN的版本号做为编译的版本信息
  • 原文地址:https://www.cnblogs.com/shengjia/p/9380204.html
Copyright © 2020-2023  润新知