如何使用类创建简单的模拟代码

2024-04-26 13:47:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用以下类创建简单的模拟代码:

product_list = []
price = []
barcode = ["banana": 123 , "apple": 234 , "orange": 345 , "pear": 456 ] 

我想用他们的代表数字在购物车中添加产品,并汇总添加的产品价格

示例:如果用户输入123,那么banana需要添加在列表中,香蕉的价格应该添加在价格列表中


Tags: 代码apple列表产品代表价格数字product
1条回答
网友
1楼 · 发布于 2024-04-26 13:47:16

看起来你需要一本python字典

例如:

barcode = {"banana": 123 , "apple": 234 , "orange": 345 , "pear": 456 }
barcode = dict((v,k) for k,v in barcode.items())    # >Reverse Key -Value
v = 123

product_list = []
price = []    

product_list.append(barcode[123])
price.append(123)

print(product_list)
print(price)

相关问题 更多 >