This is my first function in python and i need to get item quantity and price loops into the function Processorderitems. I took out the items loop because I was not doing anything with it. I need to return the subtotal and I have a 3% discount for quantities over 10. I am not sure how to loop the information into and out of the function. Do I send the list or can you send each index? I have tried both ways so far and get errors. Here is what I have now.

print "This program calculates a total after the user enters: \n the Item, Quantity and Price"
#define process function
def Processorderitem(quantitylist,pricelist):
    #discount variable so it can be changed
    discount = .03
    subtotal = 0
    for i in range(len(quantitylist)):
        for j in range(len(pricelist)):
            subtotal[i] = quantitylist[i] * pricelist[j]
    while quantity >= 10:
        subtotal *= discount
        subtotal -= discount
    return subtotal

#create lists for data
itemlist = []
quantitylist = []
pricelist = []
totlist = []
quantity = 0
price = 0
total = 0
discount = .03

#get user input
for i in range(0,2,1):
    item = str(raw_input('Enter an item: '))
    quantity = int(raw_input('How many of these do you want? '))
    price = int(raw_input('Enter the price: '))
    itemlist.append(item)
    quantitylist.append(quantity)
    pricelist.append(price)
    print itemlist,quantity,price

#loop over the lists
#    for item in enumerate(itemlist):
for quantity in enumerate(quantitylist):

    for price in enumerate(pricelist):
#call processorder function
            total = Processorderitem(quantitylist,pricelist)
#get the total
for i in (len(totlist)):
    total = Processorderitem(quantitylist,pricelist)
    print 'Item Name' + ' ' +'Quantity'+ ' ' +'Price'
    print item[i],quantity[i],total[i]

Recommended Answers

All 3 Replies

Check your post after posting. If it doesn't look right, hit the edit button and try again.

Beat you TONYJV by that much.

This is my first function in python and i need to get item quantity and price loops into the function Processorderitems. I took out the items loop because I was not doing anything with it. I need to return the subtotal and I have a 3% discount for quantities over 10. I am not sure how to loop the information into and out of the function. Do I send the list or can you send each index? I have tried both ways so far and get errors. Here is what I have now.

print "This program calculates a total after the user enters: \n the Item, Quantity and Price"
#define process function
def Processorderitem(quantitylist,pricelist):
    #discount variable so it can be changed
    discount = .03
    subtotal = 0
    for i in range(len(quantitylist)):
        for j in range(len(pricelist)):
            subtotal[i] = quantitylist[i] * pricelist[j]
    while quantity >= 10:
        subtotal *= discount
        subtotal -= discount
    return subtotal

#create lists for data
itemlist = []
quantitylist = []
pricelist = []
totlist = []
quantity = 0
price = 0
total = 0
discount = .03

#get user input
for i in range(0,2,1):
    item = str(raw_input('Enter an item: '))
    quantity = int(raw_input('How many of these do you want? '))
    price = int(raw_input('Enter the price: '))
    itemlist.append(item)
    quantitylist.append(quantity)
    pricelist.append(price)
    print itemlist,quantity,price
    
#loop over the lists
#    for item in enumerate(itemlist):
for quantity in enumerate(quantitylist):

    for price in enumerate(pricelist):
#call processorder function
            total = Processorderitem(quantitylist,pricelist)
#get the total
for i in (len(totlist)):
    total = Processorderitem(quantitylist,pricelist)
    print 'Item Name' + ' ' +'Quantity'+ ' ' +'Price'
    print item[i],quantity[i],total[i]
print "This program calculates a total after the user enters: \n the Item, Quantity and Price"
#define process function
def Processorderitem(quantitylist,pricelist):
    #discount variable so it can be changed
    discount = .03
    subtotal = 0
    for i in range(len(quantitylist)):
        for j in range(len(pricelist)):
            subtotal[i] = quantitylist[i] * pricelist[j]
    while quantity >= 10:
        subtotal *= discount
        subtotal -= discount
    return subtotal

#create lists for data
itemlist = []
quantitylist = []
pricelist = []
totlist = []
quantity = 0
price = 0
total = 0
discount = .03

#get user input
for i in range(0,2,1):
    item = str(raw_input('Enter an item: '))
    quantity = int(raw_input('How many of these do you want? '))
    price = int(raw_input('Enter the price: '))
    itemlist.append(item)
    quantitylist.append(quantity)
    pricelist.append(price)
    print itemlist,quantity,price
    
#loop over the lists
#    for item in enumerate(itemlist):
for quantity in enumerate(quantitylist):

    for price in enumerate(pricelist):
#call processorder function
            total = Processorderitem(quantitylist,pricelist)
#get the total
for i in (len(totlist)):
    total = Processorderitem(quantitylist,pricelist)
    print 'Item Name' + ' ' +'Quantity'+ ' ' +'Price'
    print item[i],quantity[i],total[i]

I would suggest using a single list with a sub-list containing [item, quantity, price]. Online explanations for lists and enumerate. Your original code slightly modified is below. Note that process_order_quantity will have to be changed to receive a single quantity and item, (which assumes that the discount is for single items > 10). You might want to return the discount and add it to the sublist as well. So as to not form bad habits, read the Python Style Guide when you have time (function names are all lower case with underscores, "_")

#get user input
items_list = []
subtotal = 0.0
for i in range(0,2,1):
    item = str(raw_input('Enter an item: '))
    quantity = int(raw_input('How many of these do you want? '))
    price = float(raw_input('Enter the price: '))
    items_list.append([item, quantity, price])   ## <----- appends a list

    ## add the subtotal
    this_subtotal = process_order_item(quantity,price)
    subtotal += this_subtotal
    print "subtotal is now", subtotal, "\n"

#loop over the lists -------------------------------------------------
#
# print each sublist
for sublist in items_list:
    print sublist

# print item, quantity, price
for this_sublist in items_list:
    print "item =", this_sublist[0], ",  quantity =", this_sublist[1], \
          ",  price =", this_sublist[2]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.