Python Forum
Working with Dictionaries - logic seems backward...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with Dictionaries - logic seems backward...
#1
Hi - I am 'taking a course' in Brilliant, and ran across this:
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.
Reply
#2
Dang, the forum software removed the indents. Oh well - it was properly structured and ran in IDLE
Reply
#3
Ha - just spotted it. The line 'inventory[fruit] -= demand' is NOT indented below the else branch, thus it runs after the inner FOR loop, and adjusts the inventory down. So inventory[fruit] -= demand lowers the inventory below zero. I initially read that as being under the 'else' branch, but that doesn't make sense, because regardless of which branch runs, the inventory needs to be updated AFTER the inner 'for' loop completes.

Oh well, guess this is a moot question now.
Reply
#4
It's just fine where it is. If you move it inside the else you will need to duplicate it for the if as well. However you need to update the inventory with num_sold, not demand

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] -= num_sold # change here
print(inventory)
you can even simplify it
for fruit, demands in daily_demands.items():
  for demand in demands:
    num_sold = min(demand, inventory[fruit]) # change here
    inventory[fruit] -= num_sold
or

for fruit, demands in daily_demands.items():
  for demand in demands:
    inventory[fruit] -= min(demand, inventory[fruit]) # change here
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
def compost(soil):
  if soil < 15:
    return soil + 10
  else:
    return soil + 5

def mulch(soil):
  new_soil = soil + 3
  if new_soil > 25:
    new_soil *= 1.2
  return round(new_soil)

def final_soil(soil):
    final = mulch(compost(soil))
    return round(final)

final = final_soil(20)

print("Final soil value:", final)
hmmm - this does not appear to work. I copied some code from a working python script in IDLE. Replied via this message. Clicked the python inserts. Pasted my code between them. Viewing it in Preview, it does not appear to have formatted correctly- everything is left justified, and no colours.
Reply
#6
You deleted the "[" from "[\python]"
print("Final soil value:", final)
/python]  # oops!
If fixed your post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Backward Calculation QuentinL 0 1,979 Nov-15-2019, 01:58 PM
Last Post: QuentinL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020