Hi - I am 'taking a course' in Brilliant, and ran across this:
However, when I run this code, the lemon demand gets subtracted from zero repeatedly, making the inventory negative.
I don't see how this is possible.
This was a quiz in Brilliant, where you selected one of two possible answers. I selected the code above, and it still seems correct to me. Tested my choice in IDLE and sure enough, it came out negative for lemons. I still don't see how.
Any thoughts?
daily_demands = { "pear": [6,4,3,2,1], "apple": [5,3,2,1,0], "orange": [8,6,5,3,2], "lemon": [16,14,11,9,7], } inventory = {"pear": 21, "apple": 20, "orange": 30, "lemon": 0} for fruit, demands in daily_demands.items(): for demand in demands: if demand > inventory[fruit]: num_sold = inventory[fruit] else: num_sold = demand inventory[fruit] -= demand print(inventory)To me, this seems to state that when demand exceeds supply, the number sold is the supply (inventory). For Lemons, the inventory starts out at zero, so any demand presented to Lemon is automatically greater than zero, and the "if" branch structure is active. num_sold = inventory(lemon) which is always zero.
However, when I run this code, the lemon demand gets subtracted from zero repeatedly, making the inventory negative.
I don't see how this is possible.
This was a quiz in Brilliant, where you selected one of two possible answers. I selected the code above, and it still seems correct to me. Tested my choice in IDLE and sure enough, it came out negative for lemons. I still don't see how.
Any thoughts?
buran write Mar-12-2025, 03:25 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
This time I added BBCode tags for you.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
This time I added BBCode tags for you.