shopping cart
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 | #Author:Kevin_hou #定义产品列表 product_list = [ ( 'HUAWEI' , 5999 ), ( 'Watch' , 500 ), ( 'Nike' , 800 ), ( 'Toyota' , 200000 ), ( 'basketball' , 500 ), ( 'bike' , 1000 ), ] shopping_list = [] #定义购物车列表 salary = input ( "your salary is...>>>" ) #首要先输入工资 if salary.isdigit(): #然后工资要是数值 salary = int (salary) #将工资取整,因为产品价格均为整数值 while True : #while True循环语句 for index,item in enumerate (product_list): #for循环语句,用于依次输出产品列表,index指数组下标值,item指具体产品 print (index,item) #输出 0 ('HUAWEI', 5999) user_choice = input ( "what do you want buy ?" ) #输入用户选择的产品,即数组值 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] #将用户选择的产品赋给p_item if p_item[ 1 ] < = salary: #再判断产品价格不大于工资值 shopping_list.append(p_item) #如果不大于,加入购车列表 salary - = p_item[ 1 ] #余额=工资-支付的费用 print ( "Add %s into shopping cart, your current balance is \033[31;1m%s\033[0m" % (p_item,salary)) #输出余额 else : print ( "\033[41;1m your current balance is [%s], you have no enough many to pay...>>>\033[0m" % salary) #输出余额不足 else : print ( "\033[41;1m product code [%s] is not exit!\033[0m" % user_choice) #输出产品不存在 elif user_choice = = 'q' : #如果用户选择输入是字母q print ( "-----------shopping list-----------" ) #输出------------shopping list------------ for p in shopping_list: #for循环,输出添加进购物车的产品 print (p) print ( "Your current balance:" , salary) #输出余额 exit() #退出 else : print ( "invalid option" ) #如果用户选择既不是在产品列表内,又不是q,则输出“invalid option”提示 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律