Click here to Skip to main content
15,923,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to write a python code that prompts user for prices, and then calculates the total, tax, etc.

it only prompts for the price twice, and then calculates the total wrong, and say that the number of prices entered is only 1 no matter what

What I have tried:

Python
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!')
Posted
Updated 1-Nov-22 11:23am
v2

Python
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):"))

Change that code to:
Python
itemCost = SENTINEL + 1
while itemCost != SENTINEL:
    itemCost = float(input("Enter the price of the item(0 to stop):"))
    if itemCost < 0:
        print(f'this price is illegal!')
    else:
        numItems+=1
        subTotal+=itemCost
 
Share this answer
 
Quote:
it only prompts for the price twice, and then calculates the total wrong, and say that the number of prices entered is only 1 no matter what

May be time to learn how to watch what your code performing: the tool is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900