# 定义初始库存数据:列表中有三个字典,每个字典代表一种水果,包括名称、单价和数量
inventory = [
{'name': '苹果', 'price': 3.5, 'count': 100},
{'name': '香蕉', 'price': 2.8, 'count': 50},
{'name': '橙子', 'price': 4.0, 'count': 80}
]
# 欢迎程序:根据输入的水果名称返回水果信息,如果不存在则返回“该水果不存在”
def welcome():
name = input("欢迎来到美味水果店!请输入要查询的水果名称:")
for fruit in inventory:
if name == fruit['name']:
print("本店有水果:{name},单价为{price}元,库存为{count}斤".format(**fruit))
return
print("该水果不存在")
# 添加水果程序:向库存添加新的水果信息
def add_fruit():
name = input("请输入水果名称:")
price = float(input("请输入水果单价:"))
count = int(input("请输入水果库存数量:"))
new_fruit = {'name': name, 'price': price, 'count': count}
inventory.append(new_fruit)
print("添加成功!")
# 修改水果信息程序:根据输入的水果名称修改单价或库存信息
def modify_fruit():
name = input("请输入要修改的水果名称:")
for fruit in inventory:
if name == fruit['name']:
price = input("请输入新的单价(直接回车表示不修改):")
if price:
fruit['price'] = float(price)
count = input("请输入新的库存数量(直接回车表示不修改):")
if count:
fruit['count'] = int(count)
print("修改成功!")
return
print("该水果不存在")
# 购买水果程序:根据输入的水果名称和购买数量计算出总金额
def buy_fruit():
name = input("请输入要购买的水果名称:")
for fruit in inventory:
if name == fruit['name']:
count = int(input("请输入购买数量:"))
if count > fruit['count']:
print("库存不足!")
return
price = fruit['price'] * count
print("您需要支付{price}元。".format(price=price))
fruit['count'] -= count
return
print("该水果不存在")
# 展示当前库存信息程序:展示目前所有水果的库存信息
def show_inventory():
print("现有库存商品为:")
print("name price count")
for fruit in inventory:
print("{name} {price} {count}".format(**fruit))
# 选择功能程序:显示菜单并根据用户输入调用相应程序
def choose_action():
while True:
print("欢迎来到美味水果店")
print("请输入您要进行的操作:")
print("1--查询")
print("2--添加")
print("3--修改")
print("4--购买")
print("5--展示")
print("其他--退出系统")
choice = input()
if choice == '1':
welcome()
elif choice == '2':
add_fruit()
elif choice == '3':
modify_fruit()
elif choice == '4':
buy_fruit()
elif choice == '5':
show_inventory()
else:
break
# 主程序:调用选择功能程序
choose_action()
fruit_shop = [
{"name": "apple", "price": 2.0, "count": 10},
{"name": "banana", "price": 3.5, "count": 5},
{"name": "orange", "price": 1.5, "count": 8},
{"name": "grape", "price": 4.0, "count": 12}
]
#添加新水果:
def add_fruit(fruit_shop, name, price, count):
for fruit in fruit_shop:
if name == fruit["name"]:
print(f"{name} already exists. Try modifying its price or count instead.")
return fruit_shop
fruit_shop.append({"name": name, "price": price, "count": count})
print(f"{name} has been added to the inventory.")
return fruit_shop
#修改现有的水果名价格数量:
def modify_fruit(fruit_shop, name, price=None, count=None):
for fruit in fruit_shop:
if name == fruit["name"]:
if price is not None:
fruit["price"] = price
if count is not None:
fruit["count"] = count
print(f"{name} has been modified.")
return fruit_shop
print(f"{name} does not exist in the inventory.")
return fruit_shop
#查询某个水果是否存在:
def search_fruit(fruit_shop, name):
for fruit in fruit_shop:
if name == fruit["name"]:
print(f"{name} exists in the inventory.")
return fruit
print(f"{name} does not exist in the inventory.")
return None
输出当前库存:
def display_inventory(fruit_shop):
print("Current Inventory:")
for fruit in fruit_shop:
print(f"Name: {fruit['name']}, Price: {fruit['price']}, Count: {fruit['count']}")
#购买水果计算总价:
def purchase_fruit(fruit_shop, name, quantity):
fruit = search_fruit(fruit_shop, name)
if fruit is not None:
if fruit["count"] < quantity:
print(f"Sorry, we only have {fruit['count']} {name}(s) left.")
return None
else:
fruit["count"] -= quantity
total_price = quantity * fruit["price"]
print(f"You have purchased {quantity} {name}(s) for a total of ${total_price:.2f}.")
return total_price
def main():
fruit_shop = [
{"name": "apple", "price": 2.0, "count": 10},
{"name": "banana", "price": 3.5, "count": 5},
{"name": "orange", "price": 1.5, "count": 8},
{"name": "grape", "price": 4.0, "count": 12}
]
print("Welcome to our Fruit Shop!")
display_inventory(fruit_shop)
add_fruit(fruit_shop, "pear", 2.5, 6)
modify_fruit(fruit_shop, "orange", price=2.0, count=10)
search_fruit(fruit_shop, "banana")
display_inventory(fruit_shop)
purchase_fruit(fruit_shop, "apple", 3)
purchase_fruit(fruit_shop, "kiwi", 2)
if __name__ == '__main__':
main()
主体部分用一个while True:不断地弹出菜单,输入非1-5时跳出循环
以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:
首先,建议你先掌握Python的基础语法,包括变量、数据类型、运算符、条件语句、循环语句等等。可以通过阅读Python教材或者网上的Python教程来学习。
针对你提出的问题,你可以先了解Python中的字符串操作。Python中的字符串可以通过索引访问单个字符,也可以通过切片获取子串。下面是一些字符串操作的示例代码:
# 定义字符串
s = "hello world"
# 获取字符串长度
print(len(s)) # 输出 11
# 获取第一个字符
print(s[0]) # 输出 h
# 获取最后一个字符
print(s[-1]) # 输出 d
# 获取子串
print(s[0:5]) # 输出 hello
# 检查字符串是否包含某个子串
print("world" in s) # 输出 True
# 将字符串转换为大写或小写
print(s.upper()) # 输出 HELLO WORLD
print(s.lower()) # 输出 hello world
另外,你还可以了解Python中的列表、字典、函数等概念和用法,这些都是Python中非常重要的概念。希望这些内容能对你有所帮助。