有些项目格式不正确,但其余的都是

时间:2017-11-15 00:31:36

标签: python

我不知道为什么有些项目格式不正确,短名称似乎没什么问题,但是较长的名字会让自己失去理智 https://pastebin.com/pkGL0Nwy

prices = {}
groceries = []

file = open("grocery_store_price_list.txt", "r")
for strx in file:
    strs = list(filter(None, strx.strip().split(" ")))
    prices[strs[0]] = [strs[1]], [strs[2]]
file.close()

file = open("my_personal_gro_list.txt", "r")
for strx in file :
    strs = list(filter(None, strx.strip().split(" ")))
    groceries.append([strs[1], strs[0]])

headings = "{:6s} {:9s} {:6s} {:7s} {:6s}".format("item", "qty", "unit", "cost", "total")

print(headings)

finalCost = 0


for strs in groceries:
    item = strs[0]
    qty = int(strs[1])
    unit = prices[strs[0]][1]
    cost = float(prices[strs[0]][0][0])
    total = qty*cost

    finalCost += total

    print(item, qty, unit, cost, total,)

1 个答案:

答案 0 :(得分:0)

print("{item}\t{qty}\t{unit}\t{cost}\t{total}".format(item=item, qty=qty, unit=unit, cost=cost, total=total))