Python Forum

Full Version: point of sale code problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python and programming, but have enjoyed the semester learning what I have... However we were asked to follow a template and build a point of sale program using a list of products, and a list of prices for those products. User input for the index of the product number, and quantity, then calculate and print a total price including a tax rate. I think I got pretty close, but I am getting an error in the function for the price calculation.

First post, sorry if I don't post it correctly

# Get app parameters and other hard-coded data:
# Assign values to named constants
TAX_RATE = 0.10
# Populate arrays with product data:
lstProducts = [
    'Iron',
    'Home Gym',
    'Microwave Oven',
    'Cordless Drill',
    'Gas Range',
    'Washer',
    'Stand Mixer',
    'Dryer',
    'Dishwasher',
    'Treadmill',
    ]
lstPrices = [
    24.95,
    794.95,
    165,
    129.95,
    495,
    399.99,
    159.95,
    349.95,
    595,
    1390
    ]

# Main Program module:
def main():
        #Call Production selection module:
        intItemNum = GetProduct()
        #Get desired quantity from end-user:
        intQuantity = int(input(f'Desired quantity: '))
        #Call Calculation module:
        fltTotalPrice = CalculatePrice()
        #Display Result:
        print (f'Total Price for this purchase is ${fltTotalPrice:.2f}')

# Subprogram modules:

def GetProduct():
        # List product indices and name:
        # Display list header:
        print (f'Product List (index - product name -> price):')
        # Initialize loop counter:
        i=0
        #Use a foreach() construct loop through name and prices arrays
        for index in range(0, len(lstProducts)):
                #Display product index and name:
                print (f"\t{i} - {lstProducts[i]} -> ${lstPrices[i]:.2f}")
                i +=1
        #Get index of desired product from end-user:
        intProdNum = int(input(f'Index of desired Product: '))
        return intProdNum

def CalculatePrice(prodIndex,pQuant):   #Perfrom Calculations:
        
        fltItemPrice = float(lstPrices[intItemNum])
        fltBasePrice = float(fltItemPrice*intQuantity)
        fltNetPrice = float(fltBasePrice*(1+(TAX_RATE)))
        return 

main()
Without having tested your code: line 15 seems to have an extra comma.
10 + 1 products, only 10 prices.
Paul
There are 3 things preventing the code from running as it is. Some of the best hints to your problems will come from the errors python gives you.

ex.
Error:
TypeError: CalculatePrice() missing 2 required positional arguments: 'prodIndex' and 'pQuant'
In main() function, the call to CalculatePrice() needs two arguments.



The other two problems are with the CalculatePrice() function. Two arguments are passed in, but are we using them?

And finally, we need to make sure that we can use the result once the price has been calculated. The function needs to send back its result so it can be displayed.
In this instance, it would be better to use a dictionary to save product.

example:
products = {
    'Iron': '24.95',
    'Home Gym': '794.95',
    'Microwave Oven': '165.00',
    'Cordless Drill': '129.95',
    'Gas Range': '495.00',
    'Washer': '399.99',
    'Stand Mixer': '159.95',
    'Dryer': '349.95',
    'Dishwasher': '595.00',
    'Treadmill': '1390.00'
 }

def get_price(product):
    return products[product]

print(f"The price of Gas range is: ${get_price('Gas Range')}")

price = get_price('Dishwasher')
print(f"Dishwasher costs: ${price}")
results:
Output:
The price of Gas range is: $495.00 Dishwasher costs: $595.00