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

How to create simple simulaton code using classes

我想使用 类 创建简单的模拟代码,例如:

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

我想通过他们的代表号码将产品添加到购物车中并总结添加的产品价格。

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

看来您需要 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)