编写一个出入库程序
时间: 2023-08-08 09:12:28 浏览: 215
以下是一个简单的出入库程序的示例:
```python
inventory = {}
def add_item(item_name, quantity):
"""
Add an item to the inventory
"""
if item_name in inventory:
inventory[item_name] += quantity
else:
inventory[item_name] = quantity
def remove_item(item_name, quantity):
"""
Remove an item from the inventory
"""
if item_name in inventory:
if inventory[item_name] >= quantity:
inventory[item_name] -= quantity
return True
else:
return False
else:
return False
def print_inventory():
"""
Print the current contents of the inventory
"""
for item_name, quantity in inventory.items():
print(f"{item_name}: {quantity}")
# Example usage
add_item("apple", 10)
add_item("banana", 5)
add_item("apple", 5)
print_inventory() # Output: apple: 15, banana: 5
remove_item("apple", 12) # Returns False, as there are only 10 apples in inventory
remove_item("banana", 3) # Returns True, as there are 2 bananas left in inventory
print_inventory() # Output: apple: 10, banana: 2
```
这是一个简单的出入库程序,使用一个字典来存储库存信息。`add_item` 和 `remove_item` 函数分别用于添加和移除库存中的物品,而 `print_inventory` 函数则用于打印当前库存信息。
阅读全文