Home > Enterprise >  Program that calculates subtotal of prices is not functioning properly
Program that calculates subtotal of prices is not functioning properly

Time:11-02

This code I have to write should display the subtotal, number of items, sales tax, and total cost

When I run it, the program calculates the prices entered wrong, and only prompts me 2 times for a price, when it should prompt me until I tell it to stop.

This program uses a while loop and sentinel to proccess the total of item costs purchased online. There is also a tax rate of 6.25% in the calculation.

SENTINEL = 0
TAXRATE = 0.0625
numItems = 0
subTotal = 0

print("""/
This program will prompt you for the items you purchased online
until you enter the sentinel value of 0 to stop the process. The
program will create a running total of the items.\n\n\n""")

itemCost = float(input("Enter the price of the item(0 to stop):"))


while itemCost != SENTINEL:
    if itemCost < 0:
        print(f'this price is illegal!')
    else:
        numItems =1
        subTotal =itemCost
    itemCost = float(input("Enter the price of the item(0 to stop):"))

    if numItems > 0:
        salesTax = subTotal * TAXRATE
        totalCost = subTotal   salesTax
        print(f'The number of items purchased is {numItems}')
        print(f'The sub total is {subTotal:.2f}')
        print(f'The sales tax is {salesTax:.2f}')
        print(f'The total cost is {totalCost:.2f}')
    else:
        print(f'You need to purchase items!')

I expected this program to prompt infinitley until you put in the sentinel number, but it only prompts twice. It also thinks that numItems always equals 1, when it shouldnt. it also calculates everything wrong.

CodePudding user response:

Indentation in Python is meaningful. Your code had the last if statement indented, but you want it to be outside the loop (you were probably also missing an \n in the opening text, where you had the /):

SENTINEL = 0
TAXRATE = 0.0625
numItems = 0
subTotal = 0

print("""\n
This program will prompt you for the items you purchased online
until you enter the sentinel value of 0 to stop the process. The
program will create a running total of the items.\n\n\n""")

itemCost = float(input("Enter the price of the item(0 to stop):"))


while itemCost != SENTINEL:
    if itemCost < 0:
        print(f'this price is illegal!')
    else:
        numItems =1
        subTotal =itemCost
    itemCost = float(input("Enter the price of the item(0 to stop):"))

if numItems > 0:
    salesTax = subTotal * TAXRATE
    totalCost = subTotal   salesTax
    print(f'The number of items purchased is {numItems}')
    print(f'The sub total is {subTotal:.2f}')
    print(f'The sales tax is {salesTax:.2f}')
    print(f'The total cost is {totalCost:.2f}')
else:
    print(f'You need to purchase items!')

With the if outside the loop, output can look like this:


This program will prompt you for the items you purchased online
until you enter the sentinel value of 0 to stop the process. The
program will create a running total of the items.



Enter the price of the item(0 to stop):1
Enter the price of the item(0 to stop):2
Enter the price of the item(0 to stop):3
Enter the price of the item(0 to stop):4.25
Enter the price of the item(0 to stop):0
The number of items purchased is 4
The sub total is 10.25
The sales tax is 0.64
The total cost is 10.89

Note that you should probably also:

  • use variable names in snake-case, i.e. num_items instead of numItems.
  • only use all-caps for global use variables (which are rarely needed), TAXRATE could be a good example, but SENTINEL isn't, as it's specific to some loops.
  • deal with input that's not a number, since it will currently crash your program.
  • deal with negative input (or at least consider that negative input is probably not an item but a discount or refund).
  • put the prompt string in a string variable, so you don't have to repeat it; or use while True: and break upon the sentinel value after prompting only once at the start of the loop - but I appreciate that you may be limited by restrictions of a specific assignment.

CodePudding user response:

I edited some lines:

1 ) I take itemCost variable in while loop. Because loop will wants input one time.

2 ) I changed your while condition and i added else condition. when itemCost == 0, loop will break.

TAXRATE = 0.0625
numItems = 0
subTotal = 0


while True:
    itemCost = float(input("Enter the price of the item(0 to stop):"))
    
    if itemCost < 0:
        print(f'this price is illegal!')
        
    elif itemCost > 0:
        numItems  = 1
        subTotal  = itemCost
    else:
        print("program finished.")
        break

    if numItems > 0:
        salesTax = subTotal * TAXRATE
        totalCost = subTotal   salesTax
        print(f'The number of items purchased is {numItems}')
        print(f'The sub total is {subTotal:.2f}')
        print(f'The sales tax is {salesTax:.2f}')
        print(f'The total cost is {totalCost:.2f}')
    else:
        print(f'You need to purchase items!')
  • Related