简单的购物车程序
简单的购物车程序,用于练习,需求如下:
启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 # @Time : 2017/9/14 16:46 4 # @Author : lichuan 5 # @File : shop_car.py 6 7 product_list={ 8 "lenove":3000, 9 "ASUS":4500, 10 "MSI":2800, 11 "HP":4200 12 } 13 FLAG_TAG=True 14 count=0 15 shop_car = {} # 已购物的商品 16 shop_list = [] # 购物车列表 17 18 while FLAG_TAG: 19 salary=input("input 'q' to exit,please input your salary:") 20 if salary == 'q': 21 FLAG_TAG=False 22 elif salary.isdigit(): 23 salary=int(salary) 24 else: 25 print("input wrong,try again!") 26 continue 27 for p in product_list: 28 print("%d,%s %d" % (count, p, product_list[p])) 29 shop_list.append(p) 30 count += 1 31 32 while FLAG_TAG: 33 product_num=input("input 'q' to exit,please input the product no:") 34 if product_num == 'q': 35 FLAG_TAG=False 36 elif product_num.isdigit() and int(product_num) < count: 37 product_num=int(product_num) 38 if salary >= product_list[shop_list[product_num]]: 39 if not shop_list[product_num] in shop_car: 40 shop_car[shop_list[product_num]]=1 41 else: 42 shop_car[shop_list[product_num]]+=1 43 salary = salary - product_list[shop_list[product_num]] 44 else: 45 print("余额不足:请选别的商品!") 46 continue 47 else: 48 print("input 'q' to exit or input a number!") 49 continue 50 print("你购买的商品如下:") 51 for s in shop_car: 52 print("%s: %d" %(s,shop_car[s])) 53 print("余额: %d" % salary)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!