在不使用列表的情况下显示项目列表

Display a list of items without using a list

我正在尝试创建一个程序,以有序的格式输入价格和税率以及 returns 商品编号、价格、税率和商品价格。 这是标准:

不允许我使用数组来解决这个问题,我必须在屏幕上将输入与输出分开。

此处显示示例输出:

Enter the price: 245.78
Enter the tax rate: 12
Are there any more items? (y/n) y
Enter the price: 34
Enter the tax rate: 10
Are there any more items? (y/n) y
Enter the price: 105.45
Enter the tax rate: 7
Are there any more items? (y/n) n


Item  Price  Tax Rate %  Item Price
====================================
1     245.78        12%      275.27
2     34.00         10%      37.40
3     105.45        7%       112.83

Total amount: 5.51

到目前为止,这是我的代码:

price= float(input('Enter the price: '))
taxRate= float(input('Enter the tax rate: '))
itemPrice = price * (1 + taxRate/100)
acc= 0
while True:
  query=input('Are there any more items? (y/n)')
  if query== 'y':
    price= float(input('Enter the price: '))
    taxRate= float(input('Enter the tax rate: '))
    itemPrice = price * (1 + taxRate/100)
    print(itemPrice )
    acc+=1
  elif query=='n':
    break
print ("Item     Price    Tax Rate%    Item Price")
print ("=========================================")
print( (acc+1) , format(price,'10.2f'), format(taxRate,'10.2f') ,
      format(itemPrice,'14.2f') )

for num in range(0,acc):
  print (acc, price, taxRate, itemPrice)

输出结果如下:


Enter the price: 245.78
Enter the tax rate: 12
Are there any more items? (y/n)y
Enter the price: 34
Enter the tax rate: 10
37.400000000000006
Are there any more items? (y/n)y
Enter the price: 105.45
Enter the tax rate: 7
112.8315
Are there any more items? (y/n)n
Item     Price    Tax Rate%    Item Price
=========================================
3     105.45       7.00         112.83
2 105.45 7.0 112.8315
2 105.45 7.0 112.8315

我很难在不使用数组的情况下尝试执行此操作。任何人都可以提供帮助吗?

您可以构建一个字符串作为输出的缓冲区吗?

像这样:

out = ""
acc = 0
totalAmount = 0
query = 'y'
while query == 'y':
    price = float(input('Enter the price: '))
    taxRate = float(input('Enter the tax rate: '))
    itemPrice = price * (1 + taxRate/100)
    acc += 1
    totalAmount += itemPrice
    out += f"{acc:>4} {price:9.2f} {taxRate:11.2f}% {itemPrice:13.2f}\n"
    query = input('Are there any more items? (y/n) ')

print("Item     Price    Tax Rate%    Item Price")
print("=========================================")
print(out)
print(f"Total amount: ${totalAmount:.2f}")

if totalAmount >= 1000:
    print("    Discount: 3%")
    print(f"Total amount: ${totalAmount*0.97:.2f} (after discount)")