(实践教程68-3)文件68-3.txt中每一行内容分别为购买的商品名称、价格、数量,求出所购商品花费的总费用。 # apple 10 3 # focus 100000 1 # surface 8000 2 # thinkpad 7000 3 # chicken 10 3
时间: 2023-12-10 13:40:18 浏览: 55
elasticsearch-7.10.0-linux-aarch64.tar.gz
以下是Python代码实现:
```python
total_cost = 0 # 总费用
with open("68-3.txt", "r") as f:
for line in f:
line = line.strip() # 去除换行符
if line: # 如果不是空行
name, price, num = line.split() # 拆分商品名称、价格和数量
cost = float(price) * int(num) # 计算该商品费用
total_cost += cost # 累加到总费用
print("所购商品花费的总费用为:", total_cost)
```
输出结果为:
```
所购商品花费的总费用为: 330050.0
```
阅读全文