以下是用 Python 编写的程序,可以帮助你生成购物清单,并自动计算每种物品的花费和总价:

items = []  # 存储物品名称
quantities = []  # 存储物品数量
prices = []  # 存储物品单价

# 输入物品信息
while True:
    item = input('请输入物品名称(输入 exit 退出):')
    if item == 'exit':
        break
    quantity = int(input('请输入物品数量:'))
    price = float(input('请输入物品单价:'))
    items.append(item)
    quantities.append(quantity)
    prices.append(price)

# 输出购物清单
print('购物清单:')
total_cost = 0
for i in range(len(items)):
    cost = quantities[i] * prices[i]
    total_cost += cost
    print('{} × {} = {:.2f}'.format(items[i], quantities[i], cost))
print('总价:{:.2f}'.format(total_cost))

使用方法:

  1. 运行程序后,按照提示输入物品名称、数量和单价。
  2. 输入 'exit' 结束输入。
  3. 程序会根据你的输入自动生成购物清单,并计算每种物品的花费和总价。

示例:

请输入物品名称(输入 exit 退出):苹果
请输入物品数量:2
请输入物品单价:3.5
请输入物品名称(输入 exit 退出):香蕉
请输入物品数量:1
请输入物品单价:2.8
请输入物品名称(输入 exit 退出):exit

输出:

购物清单:
苹果 × 2 = 7.00
香蕉 × 1 = 2.80
总价:9.80
Python 购物清单生成器:自动计算总价

原文地址: https://www.cveoy.top/t/topic/nU2d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录