python - 库存系统添加新对象而不是数量增加 1
问题描述
我正在尝试建立一个库存系统,在该系统中获取您已经拥有的物品只会增加物品的数量,而不是在列表中添加一个全新的物品。
def enc1():
print("hello")
def enc2():
print("hi")
def enc3():
print("howdy")
def enc4():
print("hey")
def enc5():
print("heyo")
class item:
def __init__(self, name, amount):
self.name = name
self.amount = amount
items = []
apple = item("apple", 1)
def addItem(itemToAdd):
for x in items:
if(x == itemToAdd):
itemToAdd.self.amount += 1
else:
items.append(itemToAdd)
addItem(apple)
print(items, apple.amount)
addItem(apple)
print(items, apple.amount)
encounter = random.randint(1,5)
if(encounter == 1):
enc1()
elif(encounter == 2):
enc2()
elif(encounter == 3):
enc3()
elif(encounter == 4):
enc4()
elif(encounter == 5):
enc5()
解决方案
推荐阅读
- css - 调整铯查看器的大小
- algorithm - 计算递归关系的时间复杂度,Big-Oh 表示法
- r - 如何使 gridarrange 中的所有地块大小相同。有些在轴上有标签,有些没有?
- kubernetes - 如何在普罗米修斯指标中复制标签
- ruby-on-rails - Rails 中的多对多选择
- magento2 - 创建 Algolia 自定义索引 magento
- python - 奇怪的结果 神经网络 Python
- java - 考虑高度计算两个坐标的Haversine距离
- c# - 在 MVC 中,如何创建将更新父模型和所有子模型的视图?
- javascript - 加载时的图像幻灯片无法正常工作