Ad

Display A List Of Items Without Using A List

I am trying to create a program that inputs the price and tax rate and returns the item number, price, tax rate, and item price in an orderly format. Here is the criteria:

  • The user can enter multiple items until the user says otherwise.
  • It calculates and display the total amount of purchased items on the bottom.
  • The price gets a 3% discount if the total amount is 1000 or more

I am not allowed to use arrays to solve this problem and I must separate the inputs from the outputs on the screen.

An example output is shown here:

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: $425.51

Here is my code so far:

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)

The output is shown as this:


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

I just have a hard time trying to do this without using an array.. can anyone be of assistance?

Ad

Answer

You could build a string as a buffer for the output?

Like this:

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)")
Ad
source: stackoverflow.com
Ad