zoukankan      html  css  js  c++  java
  • Shopping card

    这是一个购物车功能,具备判断商品展示和用户余额是否足以购买商品功能,购买商品后显示已购买的物品并给出余额提示。

    未加异常情况判断,在使用时记得添加异常情况判断。

    1、第一种方法,使用字典完成。

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Li Rong Yang
    
    commodity = {'Electric appliance':{
                    'Iphone':'5699','televison':'2300','computer':'2000'},
                 'Fruits':{
                     'cherry':'100','strawberry':'20','blueberry':'36'},
                 'Kid toy':{
                     'cats':'5000','giraffe':'3600','elephant':'3000'}
                 }
    shopping_card_list = []
    
    salary = int(input("Please enter your salary: "))#提示用户输入
    
    while True:
        print("----------- Product list ----------")
        _index_count = 0
        for cd in commodity.keys():#打印商品类型
            _index_count += 1
            print(str(_index_count) + '  ' + cd )
    
        print('')
        user_select_tepy = input("Please enter the type of item you want to buy.")#提示用户选择要购物商品类型
    
        _index_count = 0
        print("----------- Product list ----------")
        for product_name,producr_price in commodity[user_select_tepy].items():#打印商品类型中有哪些产品
            _index_count += 1
            print(str(_index_count) + " " + product_name + " " + producr_price + '¥')
    
        user_select_product = input('Please select the item you want to buy.')#让用户选择购买的产品
    
        if salary - int(commodity[user_select_tepy][user_select_product]) >0:#判断用户的工资是否大于产品价格
            print('')
    
            salary = salary- int(commodity[user_select_tepy][user_select_product])
            print('Your bank card still has a balance {balance}'.format(balance = salary))
    
    
            shopping_card_list.append(user_select_product)
            print('----------- Shopping card list -----------')
            _index_count = 0
            for SCL in shopping_card_list:
                _index_count += 1
                print(str(_index_count) + ' ' + SCL)
            print('')
    
        else:
            print('Your bank card balance is insufficient !!!')
    

    2、第二种方法,使用列表+元组完成

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Li Rong Yang
    product_list = [
        ('Iphone',5800),
        ('Mac Pro',9800),
        ('Bike',800),
        ('Watch',10600),
        ('Coffee',31),
        ('Alex Python',120),
    ]
    shopping_list = []
    salary = input("Input your salary:")
    if salary.isdigit():
        salary = int(salary)
        while True:
            for index,item in enumerate(product_list):
                #print(product_list.index(item),item)
                print(index,item)
            user_choice = input("选择要买嘛?>>>:")
            if user_choice.isdigit():
                user_choice = int(user_choice)
                if user_choice < len(product_list) and user_choice >=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 balance 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 exist!"% user_choice)
            elif user_choice == 'q':
                print("--------shopping list------")
                for p in shopping_list:
                    print(p)
                print("Your current balance:",salary)
                exit()
            else:
                print("invalid option")
    

      

  • 相关阅读:
    2020 ICPC 沈阳站 M. United in Stormwind
    [笔记] 中世纪反演魔法
    [模板] 多项式工业
    HDU 1187 Big Event in HDU
    洛谷 P2000 拯救世界
    HDU 1085 Holding Bin-Laden Captive!
    HDU 1028 Ignatius and the Princess III
    性能测试——jmeter接口测试复习——请求元件之配置元件——参数化——txt与csv
    性能测试——jmeter接口测试复习——请求元件之用户自定义变量——即参数化
    性能测试——jmeter接口测试复习——断言之响应断言——放在某个http请求下面,用来判断返回code、header、message中是否包含预期的值
  • 原文地址:https://www.cnblogs.com/lirongyang/p/10497981.html
Copyright © 2011-2022 走看看