题目:使用Python编写超市收银系统 • 商品价格如下: ◦ 1、苹果:4.5元/斤 ◦ 2、西瓜:5.5元/斤 ◦ 3、芒果:2.5元/斤 ◦ 4、荔枝:8元/斤 • 通过键盘输入对应的商品编号 • 通过键盘输入购买的商品重量(斤) • 计算购买商品总额(保留2位小数),假设:超市搞活动,全场8.5折
时间: 2024-03-21 17:41:26 浏览: 16
好的,以下是 Python 代码实现超市收银系统的功能:
```python
# 商品价格
price_dict = {
'1': 4.5,
'2': 5.5,
'3': 2.5,
'4': 8.0
}
# 通过键盘输入对应的商品编号和购买的商品重量
product_id = input("请输入商品编号(1-4):")
product_weight = float(input("请输入购买的商品重量(斤):"))
# 计算购买商品总额
product_price = price_dict.get(product_id)
total_price = product_price * product_weight * 0.85 # 全场8.5折
total_price = round(total_price, 2) # 保留2位小数
# 输出购买商品总额
print("您购买的商品总额为:{}元".format(total_price))
```
使用时,只需运行上述代码,按照提示输入商品编号和重量即可计算出购买商品的总价。注意,输入的商品编号必须为 1-4 中的一个,否则会出现 KeyError 错误。另外,输入的重量必须是一个数字,否则会出现 ValueError 错误。
相关问题
• 商品价格如下: ◦ 1、苹果:4.5元/斤 ◦ 2、西瓜:5.5元/斤 ◦ 3、芒果:2.5元/斤 ◦ 4、荔枝:8元/斤 • 通过键盘输入对应的商品编号 • 通过键盘输入购买的商品重量(斤) • 计算购买商品总额(保留2位小数),假设:超市搞活动,全场8.5折
好的,根据您的需求,可以使用以下代码实现:
```python
# 商品价格字典
price = {'1': 4.5, '2': 5.5, '3': 2.5, '4': 8}
# 获取键盘输入的商品编号和重量
product_id = input("请输入商品编号:")
weight = float(input("请输入购买重量(斤):"))
# 计算总价
total_price = round(price[product_id] * weight * 0.85, 2)
# 输出结果
print("商品编号为{},购买重量为{}斤,商品单价为{}元/斤,总价为{}元。".format(product_id, weight, price[product_id], total_price))
```
运行代码后,会提示您输入商品编号和购买重量,然后会根据输入的编号和重量计算出总价,并输出结果。其中,`round`函数用于对计算结果进行保留两位小数。
用Python编写一个程序用于水果店售货员结账:已知苹果4.50元/斤,香蕉3.60元/斤,橙子4.60元/斤。
好的,以下是一个简单的Python程序,用于计算水果店售货员结账:
```python
# 定义水果的单价
apple_price = 4.50
banana_price = 3.60
orange_price = 4.60
# 用户输入购买水果的重量
apple_weight = float(input("请输入苹果的重量(单位:斤):"))
banana_weight = float(input("请输入香蕉的重量(单位:斤):"))
orange_weight = float(input("请输入橙子的重量(单位:斤):"))
# 计算每种水果的总价和总重量
apple_total = apple_weight * apple_price
banana_total = banana_weight * banana_price
orange_total = orange_weight * orange_price
total_weight = apple_weight + banana_weight + orange_weight
# 计算总价和平均价
total_price = apple_total + banana_total + orange_total
average_price = total_price / total_weight
# 输出结果
print("苹果的总价为:%.2f 元" % apple_total)
print("香蕉的总价为:%.2f 元" % banana_total)
print("橙子的总价为:%.2f 元" % orange_total)
print("总共购买了 %.2f 斤水果,总价为:%.2f 元,平均价为:%.2f 元/斤" % (total_weight, total_price, average_price))
```
使用方法:
1. 将上述代码复制到一个Python文件中,例如 `fruit_shop.py`。
2. 在命令行中进入该文件所在的目录。
3. 运行命令 `python fruit_shop.py`。
4. 按照提示,输入购买水果的重量。
5. 程序会自动计算每种水果的总价、总重量、总价和平均价,并输出结果。
希望能对你有所帮助!