简体   繁体   中英

Python - Append index from list of orders as a tuple

I need to define a function named order_list(index_of_item, list_of_stocks, order_list) which takes an index, a list of products and a list of orders as parameters.

This function should:

-> Append a tuple consisting of the description and the price of the product at the specified index of the stock list onto the list of orders.

-> Subtract 1 from the product's quantity value in the stock list.

-> If the quantity is now 0, remove the product from the stock list.

Here is an example of the working of this function -

TEST:

items = ['11,Coca Cola Soft Drink 500ml,4,2', '12,L & P Soft Drink Lemon & Paeroa 500ml,4,1', '13,V Blue Drink can 500mL,3.5,8', '14,V Vitalise Energy Drink 500ml,3.5,5']
orders = []
add_to_orders(0, items, orders)
add_to_orders(1, items, orders)
print(orders)
print(items[0])
print(items[1])

Items in the product list are described as follows -

'11,Coca Cola Soft Drink 500ml,4,2'

Here 11 is the code number of that product, coca cola soft drink 500ml - product name, 4 is the price of the product ($4) and 2 is the quantity of the product.

OUTPUT EXPECTED:

[('Coca Cola Soft Drink 500ml', '4'), ('L & P Soft Drink Lemon & Paeroa 500ml', '4')]
11,Coca Cola Soft Drink 500ml,4,1
13,V Blue Drink can 500mL,3.5,8

This is the code I wrote but this only prints the first line correctly. I don't know how to alter the value of the quantity of products (subtract 1)

def order_list(index_of_item, list_of_stocks, order_list) :
    list1 = []
    for index in range(len(list_of_stocks)):
        list_of_items = list_of_stocks[index].split(",")
        item = list_of_items[1]
        price = list_of_items[2]
        qty = int(list_of_stocks[index][-1])
        list1.append((item, price))
        
    orders.append(list1[index_of_item])

**OUTPUT GOT- ** 在此处输入图像描述

try:

def add_to_orders(index_of_item, list_of_stocks, order_list):
    item = list_of_stocks[index_of_item]
    code, name, price, quantity = item.split(',')
    quantity = int(quantity)

    order_list.append((name, price))

    quantity -= 1

    if quantity >= 1:
        list_of_stocks[index_of_item] = ','.join([code, name, price, str(quantity)])
    else:
        del list_of_stocks[index_of_item]

explanation:

I first pulled the different items out of the stock according to the provided index and using split . Then I convert only the quantity to integer because we will subtract 1 from it.

Up until now you are ready to append the tuple you want to orders 's list.

After that you do a simply check to see if the quantity reaches zero or not. If it doesn't, build a new string in the original format using ','.join([code, name, price, str(quantity)])

test:

items = ['11,Coca Cola Soft Drink 500ml,4,2',
         '12,L & P Soft Drink Lemon & Paeroa 500ml,4,1',
         '13,V Blue Drink can 500mL,3.5,8',
         '14,V Vitalise Energy Drink 500ml,3.5,5']

orders = []
add_to_orders(0, items, orders)
add_to_orders(1, items, orders)
print(orders)
print(items[0])
print(items[1])

output:

[('Coca Cola Soft Drink 500ml', '4'), ('L & P Soft Drink Lemon & Paeroa 500ml', '4')]
11,Coca Cola Soft Drink 500ml,4,1
13,V Blue Drink can 500mL,3.5,8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM