zoukankan      html  css  js  c++  java
  • Python基础练习之购物车

    #前置知识点
    # enumerate(LIST)
    #  输出一个元组,第一个为下标,第二个为元素
    
    # a = [1, 2, 3, 4]
    # for i in enumerate(a):
    #     print(i)
    '''
    (0, 1)
    (1, 2)
    (2, 3)
    (3, 4)
    '''
    # for i, item in enumerate(a):
    #     print(i, item)
    '''
    0 1
    1 2
    2 3
    3 4
    '''
    #len(LIST)  #返回列表长度
    
    # print('33[31;1mCONTENT33[0m')   
    
    #输出为红色的CONTENT
    
    #exit()  #退出方法
    
    
    #=====================
    #Begin
    #
    productList = [
        ('iphone', 5000),
        ('watch', 20000),
        ('coffee', 1000),
        ('pencil', 500),
        ('switch', 2000),
        ('Audi', 200000),
    ]
    purchasedList = []
    
    salary = input('请输入你的工资金额:')
    if salary.isdigit():
        salary = int(salary)
        while True:
            for i, item in enumerate(productList):
                print(i, item)
    
            userChoice = input('要购买东西吗?')
            if userChoice.isdigit():
                userChoice = int(userChoice)
                if userChoice < len(productList) and userChoice >= 0:
                    userWantBuy = productList[userChoice]
                    if userWantBuy[1] <= salary:
                        purchasedList.append(userWantBuy)
                        print('已添加',userWantBuy[0],'到购物车')
                        salary -= userWantBuy[1]
                    else:
                        print('你的钱不够哇!')
                else:
                    print('你选择的:',userChoice,'商品不存在!')
            elif userChoice.lower() == 'q':
                print('=======已经购买的商品=======')
                for i in purchasedList:
                    print(i)
                print('你的余额为:33[31;1m{}33[0m'.format(salary))
                break    
            else:
                print('输入错误啦')
    else:
        print('工资格式不对!')
        exit(-1)
    

      

  • 相关阅读:
    20145201 《Java程序设计》第四周学习总结
    20145201 《Java程序设计》第三周学习总结
    20145201 《Java程序设计》第二周学习总结
    20145201 《Java程序设计》第一周学习总结(修改)
    输入与输出
    Fibonacci
    Collection与Map
    异常处理
    接口和多态
    继承与多态
  • 原文地址:https://www.cnblogs.com/lcxiao/p/11367110.html
Copyright © 2011-2022 走看看