我有这段代码。
item = stock.stock_list(location)
for lists in sorted(item):
print ("{:<20}".format(lists))
for price in sorted(item):
print("{:>8.2f}".format(stock_price(price)))
for qty in sorted(item):
print("{:>6}".format(stock.stock_quantity(location, qty))
并打印出来,
Beetroot
Black-eyed peas
Cassava
Greater plantain
Pak choy
17.03
11.98
11.61
10.09
92
94
76
67
我需要它在同一行打印出来,就像这样。
Beetroot 17.03 94
Black-eyed peas 11.98 92
Cassava 43.21 76
Greater plantain 12.45 43
Pak choy 19.22 43
不要担心数字。 它必须像这样对齐,
"{:<20}{:>8.2f}{:>6}".format
答案 0 :(得分:3)
只需使用一个循环:
for i in sorted(item):
print("{:<20}{:>8.2f}{:>6}".format(i,
stock_price(i),
stock.stock_quantity(location, i))
您可能想要一个比i
更好的名称,但这只是白板代码
答案 1 :(得分:1)
我认为就是你所追求的(虚拟代码):
import random
item = ['Beetroot',
'Black-eyed peas',
'Cassava',
'Greater plantain'
'Pak choy']
for an_item in sorted(item):
stoc_price = random.randint(1,10)
stock_quantity = random.randint(1,10)
print("{:<30}{:>8.2f}{:>6}".format(an_item, stoc_price, stock_quantity))
给出:
Beetroot 5.00 1
Black-eyed peas 2.00 8
Cassava 1.00 5
Greater plantainPak choy 6.00 9