* * * 文章内容仅是作为个人学习记录,如有不足,欢迎指正♥
posts - 19,comments - 0,views - 10276
复制代码

创建ComputerShop类,

参数:list列表,存储内容有库存、品牌、价格(例如:

computerShop = [{"count": 100, 'brand': "拯救者", "price": 2000},{"count": 100, 'brand': "外星人", "price": 8100}]

),money为用户开店进货后剩余金额,创建对象时需要指定该金额。
求:商品进货,输入进货名称,进货价为商品价格-1000,判断店铺金额是否满足进货金额,不满足重新输入,满足后,店铺余额减少,指定商品数量增加


# 类的使用,成员变量,成员方法的使用,以及for循环的应用
class
ComputerShop(): # 商品的标准价格,库存,名称 computerShop = [ {"count": 100, 'brand': "拯救者", "price": 2000}, {"count": 100, 'brand': "外星人", "price": 8100} ] def __init__(self, money): # 店铺的总库存 self.product_list = [] # 店铺的余额 self.money = money # 进货 def jinhuo(self,brand,count): for c in ComputerShop.computerShop: # 进货加库存价,余额减 if c["brand"] == brand: # 计算总的进货价钱 total_price = (c["price"] - 1000) * count if self.money >= total_price: self.money = self.money - total_price # 钱充足的情况下,库存增加,注意⚠️此处,循环列表,每添加完一个要结束,以及循环列表所有的商品,全部找不到才会执行else,才可以手动增加商品 for i in self.product_list: if i['brand'] == c['brand']: i["count"] += count break else: self.product_list.append({"count": count, 'brand': brand, "price": c['price']}) else: print("资金不足") break else: print("进货的商品不存在") my = ComputerShop(50000) my.jinhuo("拯救者", 1) my.jinhuo("拯救者1", 1) print(my.product_list) print(my.money)
复制代码

 

posted on   __陈胖胖  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示