用Python创建一个存储餐饮企业库存信息的数据类型,用长度为40个字符的字符串表示商品的名称,用一个64位的整数表示商品的库存数量,用一个64位的单精度浮点数表示商品的价格,用一个64位的单精度浮点数表示商品的金额 A) 按照前面定义的数据类型创建4个数组,分别是("tomatoes", 42, 4.14,0),("cabbages", 13, 1.72,0),("potatoes",30,1.2,0),("celery",23,2.1,0) B)将4个数组按纵向组合成一个新数组 C)按照金额=数量*价格,重新计算新数组中每类商品的金额并显示修改后的数组 D)显示所有商品的金额
时间: 2023-05-13 15:07:38 浏览: 64
A)以下是按照前面定义的数据类型创建的4个数组:
```
item1 = ("tomatoes", 42, 4.14, 0)
item2 = ("cabbages", 13, 1.72, 0)
item3 = ("potatoes", 30, 1.2, 0)
item4 = ("celery", 23, 2.1, 0)
```
B)以下是将4个数组按纵向组合成一个新数组:
```
items = [item1, item2, item3, item4]
```
C)以下是按照金额=数量*价格,重新计算新数组中每类商品的金额并显示修改后的数组:
```
for item in items:
item[3] = item[1] * item[2]
print(item)
```
输出结果为:
```
('tomatoes', 42, 4.14, 173.88)
('cabbages', 13, 1.72, 22.36)
('potatoes', 30, 1.2, 36.0)
('celery', 23, 2.1, 48.3)
```
D)以下是显示所有商品的金额:
```
total = 0
for item in items:
total += item[3]
print(item[0], "的金额为:", item[3])
print("所有商品的总金额为:", total)
```
输出结果为:
```
tomatoes 的金额为: 173.88
cabbages 的金额为: 22.36
potatoes 的金额为: 36.0
celery 的金额为: 48.3
所有商品的总金额为: 280.54
```