【发布时间】:2019-11-22 12:16:36
【问题描述】:
提供的是有关商店库存的数据列表,其中列表中的每个项目都代表项目的名称、库存量以及成本。使用 .format 方法(不是字符串连接)以相同的格式打印出列表中的每个项目。例如,第一个打印语句应为商店有 12 只鞋子,每只 29.99 美元。
下面是我尝试使用的代码。
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25,
15.00", "scarves, 13, 7.75"]
for person in inventory:
item = person[0]
quantity = person[1]
amount = person[2]
print("The store has {} {}, each for {} USD.".format(item, quantity,
amount))
我不明白为什么它只抓住单词的第一个字母。下面是我的输出。 输出:
The store has s h, each for o USD.
The store has s h, each for i USD.
The store has s w, each for e USD.
The store has s c, each for a USD.
【问题讨论】:
标签: python arrays regex python-3.x string