
学了一天没有一点头绪 .到底该怎么做初学者实在搞不懂 . 刚接触还有好多不懂

# 定义初始库存数据:列表中有三个字典,每个字典代表一种水果,包括名称、单价和数量
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()