-1

Hi I'm new to Python and I have this list of list:

product_list=[[0, 'Cool Blue Marella Jug', 33, 15], [1, 'Weight Loss Pack', 55, 16], [2, 'Weight Loss Pack', 10, 16]]

and the first number is basically the index, the second number(33,55,10) is the price and the third number(15,16,16) is the profit. Given a price limit of say 15 my code should print the max profit which is obviously 16 in this case. But it prints 32 instead?

This is my code:

def dp_pricelimit(product_list, price_limit):
   memo=[0]*(price_limit +1)
   memo[0]=0
   for price in range(1, price_limit +1):
        for item in product_list:#go through the items
            if item[2]<=price_limit:
                balance=price_limit-item[2]
                profit=item[3] + memo[balance]
             if profit>memo[price]:#if found new optimal
                 memo[price]=profit
    return memo[price_limit]

Any help will be appreciated. Thanks!

Sook Lim
  • 541
  • 6
  • 28
  • Sorry it compiles but the identations were off when I tried formatting it here – Sook Lim Aug 23 '18 at 08:35
  • It's still off... – user202729 Aug 23 '18 at 08:36
  • Well can u tell me where is it off? Is it so off that u can't tell which if statements and loops go where? All I know is my code compiles but its giving me 32 instead of 16 which is the correct answer. – Sook Lim Aug 23 '18 at 08:51
  • 1
    even with right indentations I get an UnboundLocalError because the variable profit is referenced in the if before assignment if I run your code with product_list as above and price_limit at 15 – Sharku Aug 23 '18 at 08:52

1 Answers1

2

Okay sorry guys I just realised it should be price instead of price_limit. For the if statement

if item[2]<=price_limit 

My bad.And the line after it should be price as well.

Sook Lim
  • 541
  • 6
  • 28