农产品信息管理代码
时间: 2023-07-02 22:08:17 浏览: 37
以下是一个简单的农产品信息管理程序示例,使用Python语言编写:
```python
# 农产品信息管理程序
# 初始化农产品列表
products = []
# 添加农产品信息函数
def add_product():
name = input("请输入农产品名称:")
price = float(input("请输入价格:"))
quantity = int(input("请输入数量:"))
product = {
"name": name,
"price": price,
"quantity": quantity
}
products.append(product)
print("添加成功!")
# 显示所有农产品信息函数
def show_products():
if len(products) == 0:
print("没有任何农产品信息!")
else:
print("名称\t\t价格\t\t数量")
for product in products:
print(f"{product['name']}\t\t{product['price']}\t\t{product['quantity']}")
# 搜索农产品函数
def search_product():
name = input("请输入要搜索的农产品名称:")
found = False
for product in products:
if product["name"] == name:
found = True
print("名称\t\t价格\t\t数量")
print(f"{product['name']}\t\t{product['price']}\t\t{product['quantity']}")
if not found:
print("没有找到该农产品!")
# 修改农产品信息函数
def modify_product():
name = input("请输入要修改的农产品名称:")
found = False
for product in products:
if product["name"] == name:
found = True
price = float(input("请输入新的价格:"))
quantity = int(input("请输入新的数量:"))
product["price"] = price
product["quantity"] = quantity
print("修改成功!")
if not found:
print("没有找到该农产品!")
# 删除农产品信息函数
def delete_product():
name = input("请输入要删除的农产品名称:")
found = False
for product in products:
if product["name"] == name:
found = True
products.remove(product)
print("删除成功!")
if not found:
print("没有找到该农产品!")
# 主程序循环
while True:
print("\n欢迎使用农产品信息管理程序!")
print("1. 添加农产品信息")
print("2. 显示所有农产品信息")
print("3. 搜索农产品")
print("4. 修改农产品信息")
print("5. 删除农产品信息")
print("0. 退出程序")
choice = input("请选择操作:")
if choice == "1":
add_product()
elif choice == "2":
show_products()
elif choice == "3":
search_product()
elif choice == "4":
modify_product()
elif choice == "5":
delete_product()
elif choice == "0":
print("谢谢使用,再见!")
break
else:
print("无效的操作,请重新选择!")
```
这个程序可以让用户添加、显示、搜索、修改和删除农产品信息。农产品信息以字典形式存储在一个列表中。用户可以通过输入菜单选项来执行相应的操作。