Mar-14-2025, 06:14 PM
Hi!
I have following task:
Now, what I did:
For better understanding of the issue, this is what nested loops code returns:
I have following task:
Quote:The following code cell will execute a nested loop that will deliver all possible combinations of the elements from the products_on_sale, sale_prices and quantities lists:
products_on_sale = ['Chair_Type_1', 'Chair_Type_2', 'Chair_Type_3', 'Chair_Type_4'] sale_prices = [100, 120, 135, 150] quantities = [1000, 1500, 1300] for chair_type in products_on_sale: for price in sale_prices: for quantity in quantities: print ([chair_type, price*quantity])Use a list comprehension to obtain the same output. Store it in a variable called sales_revenue.
Now, what I did:
products_on_sale = ['Chair_Type_1', 'Chair_Type_2', 'Chair_Type_3', 'Chair_Type_4'] sale_prices = [100, 120, 135, 150] quantities = [1000, 1500, 1300] sales_revenue = [[chair_type, price * quantity] for chair_type in products_on_sale for price in sale_prices for quantity in quantities] sales_revenueMy code isn't accepted by the learning platform (Udemy) although it's generally consistent. Please note, my code returns nested list of lists, whereas the code with nested loops returns just loose range of lists (see also outputs pasted below). I guess I should provide a solution where the result is identical like in nested loops. I have no access to the author of this task, I can only test the code. Error details of the test are not very sophisticated though:
Error details '' != 'Chair_Type_1 100000\nChair_Type_1 150000\[963 chars]00\n' Diff is 1057 characters long. Set self.maxDiff to None to see it.I doubt if I can have any other approach for the list comprehension case as it seems it always returns a list (even with a range of sub-lists). But then what am I doing wrong? I asked ChatGPT for a help, but it gives me stupid answers with identical code like mine. I will be thankful for a solution.
For better understanding of the issue, this is what nested loops code returns:
['Chair_Type_1', 100000] ['Chair_Type_1', 150000] ['Chair_Type_1', 130000] ['Chair_Type_1', 120000] ['Chair_Type_1', 180000] ['Chair_Type_1', 156000] ['Chair_Type_1', 135000] ['Chair_Type_1', 202500] ['Chair_Type_1', 175500] ['Chair_Type_1', 150000] ['Chair_Type_1', 225000] ['Chair_Type_1', 195000] ['Chair_Type_2', 100000] ['Chair_Type_2', 150000] ['Chair_Type_2', 130000] ['Chair_Type_2', 120000] ['Chair_Type_2', 180000] ['Chair_Type_2', 156000] ['Chair_Type_2', 135000] ['Chair_Type_2', 202500] ['Chair_Type_2', 175500] ['Chair_Type_2', 150000] ['Chair_Type_2', 225000] ['Chair_Type_2', 195000] ['Chair_Type_3', 100000] ['Chair_Type_3', 150000] ['Chair_Type_3', 130000] ['Chair_Type_3', 120000] ['Chair_Type_3', 180000] ['Chair_Type_3', 156000] ['Chair_Type_3', 135000] ['Chair_Type_3', 202500] ['Chair_Type_3', 175500] ['Chair_Type_3', 150000] ['Chair_Type_3', 225000] ['Chair_Type_3', 195000] ['Chair_Type_4', 100000] ['Chair_Type_4', 150000] ['Chair_Type_4', 130000] ['Chair_Type_4', 120000] ['Chair_Type_4', 180000] ['Chair_Type_4', 156000] ['Chair_Type_4', 135000] ['Chair_Type_4', 202500] ['Chair_Type_4', 175500] ['Chair_Type_4', 150000] ['Chair_Type_4', 225000] ['Chair_Type_4', 195000]And this is what my code with list comprehension returns:
[['Chair_Type_1', 100000], ['Chair_Type_1', 150000], ['Chair_Type_1', 130000], ['Chair_Type_1', 120000], ['Chair_Type_1', 180000], ['Chair_Type_1', 156000], ['Chair_Type_1', 135000], ['Chair_Type_1', 202500], ['Chair_Type_1', 175500], ['Chair_Type_1', 150000], ['Chair_Type_1', 225000], ['Chair_Type_1', 195000], ['Chair_Type_2', 100000], ['Chair_Type_2', 150000], ['Chair_Type_2', 130000], ['Chair_Type_2', 120000], ['Chair_Type_2', 180000], ['Chair_Type_2', 156000], ['Chair_Type_2', 135000], ['Chair_Type_2', 202500], ['Chair_Type_2', 175500], ['Chair_Type_2', 150000], ['Chair_Type_2', 225000], ['Chair_Type_2', 195000], ['Chair_Type_3', 100000], ['Chair_Type_3', 150000], ['Chair_Type_3', 130000], ['Chair_Type_3', 120000], ['Chair_Type_3', 180000], ['Chair_Type_3', 156000], ['Chair_Type_3', 135000], ['Chair_Type_3', 202500], ['Chair_Type_3', 175500], ['Chair_Type_3', 150000], ['Chair_Type_3', 225000], ['Chair_Type_3', 195000], ['Chair_Type_4', 100000], ['Chair_Type_4', 150000], ['Chair_Type_4', 130000], ['Chair_Type_4', 120000], ['Chair_Type_4', 180000], ['Chair_Type_4', 156000], ['Chair_Type_4', 135000], ['Chair_Type_4', 202500], ['Chair_Type_4', 175500], ['Chair_Type_4', 150000], ['Chair_Type_4', 225000], ['Chair_Type_4', 195000]]