Shopping card
这是一个购物车功能,具备判断商品展示和用户余额是否足以购买商品功能,购买商品后显示已购买的物品并给出余额提示。
未加异常情况判断,在使用时记得添加异常情况判断。
1、第一种方法,使用字典完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #!/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、第二种方法,使用列表+元组完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/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 \033[31;1m%s\033[0m" % (p_item,salary) ) else : print ( "\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[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" ) |
分类:
Python小程序
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异