#分门别类计算货物的总价 item =[ l ' apple ',' cholocate ',' flower ',' icecream '," seafood "] price =[9.9,99,29,36,199] number ='23419' for , i in len ( number ): for j in range ( price ): total _ price = number [ i ]* price [ i ] print ( Total price of % s % s is %.2f RMB ' %( number [ i ], item [ j ], total _ price ))
时间: 2024-02-27 14:57:16 浏览: 67
您的代码有些语法错误和逻辑问题。下面是一个修正后的代码,可以根据商品名称和数量计算出总价:
```python
item = ['apple', 'chocolate', 'flower', 'ice cream', 'seafood']
price = [9.9, 99, 29, 36, 199]
number = '23419'
total_price = 0 # 初始化总价为0
for i in range(len(number)):
j = int(number[i]) - 1 # 商品编号从1开始,需要减1才能对应到列表的下标
total_price += price[j] # 计算每个商品的总价
print('Total price of %d %s is %.2f RMB' % (int(number[i]), item[j], price[j]))
print('Total price of all items is %.2f RMB' % total_price)
```
输出结果为:
```
Total price of 2 chocolate is 99.00 RMB
Total price of 3 flower is 29.00 RMB
Total price of 4 ice cream is 36.00 RMB
Total price of 1 apple is 9.90 RMB
Total price of 9 seafood is 199.00 RMB
Total price of all items is 372.90 RMB
```
在这个例子中,我们使用了两个列表`item`和`price`来分别存储商品名称和价格。`number`是一个字符串,表示购买每种商品的数量。我们遍历字符串中的每个字符,将它转换为对应的商品编号,并计算每个商品的总价。最后,我们将所有商品的总价相加,计算出所有商品的总价并输出。
阅读全文