Write a program to manage inventory of goods. The specific functions are: (1) Enter the quantity of products and the name, number, and inventory quantity of each product through the keyboard. (2) Save the inventory quantity of the product to a text file.
时间: 2024-03-06 20:47:09 浏览: 74
商品管理程序_可保存商品数据
Sure, I can help you with that. Here's a Python program that can manage inventory of goods as per your requirements:
```python
# function to enter the product details through keyboard
def enter_details():
num_products = int(input("Enter the number of products: "))
products = []
for i in range(num_products):
product = {}
product['name'] = input(f"Enter the name of product {i+1}: ")
product['number'] = input(f"Enter the number of product {i+1}: ")
product['quantity'] = int(input(f"Enter the inventory quantity of product {i+1}: "))
products.append(product)
return products
# function to save the inventory quantity to a text file
def save_inventory(products):
with open("inventory.txt", "w") as f:
for product in products:
f.write(f"{product['name']},{product['number']},{product['quantity']}\n")
# main function
def main():
products = enter_details()
save_inventory(products)
print("Inventory details saved to file 'inventory.txt'.")
if __name__ == "__main__":
main()
```
When you run this program, it will first ask you to enter the number of products and then ask you to enter the name, number, and inventory quantity of each product through the keyboard. After entering the details, it will save the inventory quantity of each product to a text file named 'inventory.txt'.
You can modify this program as per your specific requirements. Let me know if you have any questions or need further assistance.
阅读全文