#๐Ÿ”’ comparing dictionaries then updating based on quantities

62 messages ยท Page 1 of 1 (latest)

lean parcel
#

i'm not sure where i'm going wrong here

sick notchBOT
#

@lean parcel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lean parcel
#
def grocery_cost(input_value):
    """docstring info for function #2"""
    stock = {
    "tomato soup": 20,
    "cheese": 10,
    "bread": 3,
    "milk": 1,
    "butter": 7,
    "coffee": 8,
    "ice cream": 5,
    "orange juice":  12,
    "bacon": 2,
    "tortilla chips":  4,
    "ramen":  20
    }

    prices = {
    "tomato soup": 1.85,
    "cheese": 3.99,
    "bread": 2.50,
    "milk": 3.59,
    "butter": 1.99,
    "coffee": 5.99,
    "ice cream": 2.99,
    "orange juice":  2.50,
    "bacon": 5.49,
    "tortilla chips":  3.99,
    "ramen":  0.99
    }
    total_cost = 0
    nonstock_items = {}
    items_sold = {}
    insufficient_quantities = {}
    for n in input_value:
        if n not in stock:
            nonstock_items.update(n)
            del n
        else:
            for i in stock:
                if n == i:
                    quantity = 0
                    if stock[i]-input_value[n] < 0:
                        request = input(f"amount requested greater than amount in stock. would you like to change amount requested to {stock[i]} \ntype yes if you wish to do so")
                        if request == 'yes':
                            input_value[n] = stock[i]
                        else:
                            insufficient_quantities[n] = input_value[n]
                            del n
                    quantity = stock[i] - input_value[n]
                    items_sold[n] = quantity
    print(items_sold)
silent nebula
#

Error messages? Output? What is the type of input_value?

lean parcel
#

type of input is dictionary

silent nebula
#

You should also use more descriptive names than n and i. Like stock_key for i.

#

A dictionary mapping what to what?

lean parcel
#

when i enter no instead of yes for input within the function, i'm getting an UnboundLocalError

silent nebula
#

!traceback

Please post to full output and traceback.

sick notchBOT
#
Traceback

Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.

A full traceback could look like:

Traceback (most recent call last):
  File "my_file.py", line 5, in <module>
    add_three("6")
  File "my_file.py", line 2, in add_three
    a = num + 3
        ~~~~^~~
TypeError: can only concatenate str (not "int") to str

If the traceback is long, use our pastebin.

lean parcel
#

input dictionary is basically an order from stock. need to catch if quantity in input dictionary is greater than stock quantity and prompt user to say if they want the full stock quantity

#

UnboundLocalError: cannot access local variable 'n' where it is not associated with a value

silent nebula
lean parcel
#

how do i do that

#

ah one sec

silent nebula
lean parcel
#

Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
grocery_cost({"tomato soup": 25,"cheese": 1,"bread": 2,"butter": 7,"taters": 32,"coffee": 8,"ice cream": 3,"orange juice": 12,"bacon": 2,"tortilla chips": 8,"ramen": 20})
File "C:\Users\kyle\Desktop\HW2.py", line 128, in grocery_cost
quantity = stock[i] - input_value[n]
UnboundLocalError: cannot access local variable 'n' where it is not associated with a value

silent nebula
#

Is that the current code? because it doesn't look like n can be undefined at that line.

silent nebula
#

Also the code you cited doesn't even have 128 lines.

lean parcel
#

yes. i still have some adjustments to make. like i have "taters" in the input dictionary but not in stock, so i need to catch that. but the function works correctly otherwise unless i type "no" when prompted

#

i have multiple functions in one file. it's a homework assignment

silent nebula
lean parcel
lean parcel
silent nebula
#

So we can match up your traceback line numbers with the correct code.

lean parcel
#

alright

#

its too long. i'll make a new file

silent nebula
#

Ok. it's always good to reduce a problem to the smallest thing you can which shows the problem.

lean parcel
#

Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
grocery_cost({"tomato soup": 25,"cheese": 1,"bread": 2,"butter": 7,"taters": 32,"coffee": 8,"ice cream": 3,"orange juice": 12,"bacon": 2,"tortilla chips": 8,"ramen": 20})
File "C:/Users/kyle/Desktop/yyyyyyyyyyyy.py", line 49, in grocery_cost
quantity = stock[i] - input_value[n]
UnboundLocalError: cannot access local variable 'n' where it is not associated with a value

silent nebula
#

Cool. And post yyyyyyyyyyyy.py ?

lean parcel
#

it's the same right?

silent nebula
#

I don't know that! I want to see the actualy code which caused the traceback above.

lean parcel
#
def grocery_cost(input_value):
    """docstring info for function #2"""
    stock = {
    "tomato soup": 20,
    "cheese": 10,
    "bread": 3,
    "milk": 1,
    "butter": 7,
    "coffee": 8,
    "ice cream": 5,
    "orange juice":  12,
    "bacon": 2,
    "tortilla chips":  4,
    "ramen":  20
    }

    prices = {
    "tomato soup": 1.85,
    "cheese": 3.99,
    "bread": 2.50,
    "milk": 3.59,
    "butter": 1.99,
    "coffee": 5.99,
    "ice cream": 2.99,
    "orange juice":  2.50,
    "bacon": 5.49,
    "tortilla chips":  3.99,
    "ramen":  0.99
    }
    total_cost = 0
    nonstock_items = {}
    items_sold = {}
    insufficient_quantities = {}
    for n in input_value:
        if n not in stock:
            nonstock_items.update(n)
            del n
        else:
            for i in stock:
                if n == i:
                    quantity = 0
                    if stock[i]-input_value[n] < 0:
                        request = input(f"amount requested greater than amount in stock. would you like to change amount requested to {stock[i]} \ntype yes if you wish to do so")
                        if request == 'yes':
                            input_value[n] = stock[i]
                        else:
                            insufficient_quantities[n] = input_value[n]
                            del n
                    quantity = stock[i] - input_value[n]
                    items_sold[n] = quantity
    print(items_sold)
pale lodge
#
del n

This is very strange. You almost never want to delete a name. Did you intend to delete from a list there?

lean parcel
#

i'm trying to delete both the key and value from input_value dictionary

silent nebula
#

But right after the if you use both n and input_value[n]

lean parcel
#

input_value[n] is the value not the key right?

pale lodge
# lean parcel i'm trying to delete both the key and value from input_value dictionary

I think you intended to do del insufficient_quantities[n] then. del n and del name[n] are completely different despite the fact that they look similar. del n deletes the name n from the namespace, meaning you cannot use n after that until it's reassigned. del name[n] deletes the key and value from the object that name refers to (a dictionary here).

silent nebula
#

I suspect the OP wanted to del input_value[n], but shouldn't be trying to adjust the quantity if they do so.

pale lodge
#

Ya, whoops, had the wrong dictionary name.

lean parcel
#

del input_value[n] will only be deleting the value or it will delete both value and key?

silent nebula
#

It deletes the entry with key n, so that deletes the key and its value.

lean parcel
#

Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
grocery_cost({"tomato soup": 25,"cheese": 1,"bread": 2,"butter": 7,"taters": 32,"coffee": 8,"ice cream": 3,"orange juice": 12,"bacon": 2,"tortilla chips": 8,"ramen": 20})
File "C:/Users/kyle/Desktop/yyyyyyyyyyyy.py", line 49, in grocery_cost
quantity = stock[i] - input_value[n]
KeyError: 'tomato soup'

#
def grocery_cost(input_value):
    """docstring info for function #2"""
    stock = {
    "tomato soup": 20,
    "cheese": 10,
    "bread": 3,
    "milk": 1,
    "butter": 7,
    "coffee": 8,
    "ice cream": 5,
    "orange juice":  12,
    "bacon": 2,
    "tortilla chips":  4,
    "ramen":  20
    }

    prices = {
    "tomato soup": 1.85,
    "cheese": 3.99,
    "bread": 2.50,
    "milk": 3.59,
    "butter": 1.99,
    "coffee": 5.99,
    "ice cream": 2.99,
    "orange juice":  2.50,
    "bacon": 5.49,
    "tortilla chips":  3.99,
    "ramen":  0.99
    }
    total_cost = 0
    nonstock_items = {}
    items_sold = {}
    insufficient_quantities = {}
    for n in input_value:
        if n not in stock:
            nonstock_items[n] = input_value[n]
            del input_value[n]
        else:
            for i in stock:
                if n == i:
                    quantity = 0
                    if stock[i]-input_value[n] < 0:
                        request = input(f"{n} amount requested greater than amount in stock. would you like to change amount requested to {stock[i]} \ntype yes if you wish to do so ")
                        if request == 'yes':
                            input_value[n] = stock[i]
                        else:
                            insufficient_quantities[n] = input_value[n]
                            del input_value[n]
                    quantity = stock[i] - input_value[n]
                    items_sold[n] = quantity
    print(items_sold)
silent nebula
#

Consider: you went del input_value[n].
Then you try to go: quantity = stock[i] - input_value[n]. That's not going to work after the del.

lean parcel
#

yea i know but i'm not sure how to correct my code to avoid quantity = stock[i] - input_value[n] without breaking from the for loop

silent nebula
#

If you del the input value, go continue to do the next loop iteration, skipping the stuff below.

lean parcel
#

Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
grocery_cost({"tomato soup": 25,"cheese": 1,"bread": 2,"butter": 7,"taters": 32,"coffee": 8,"ice cream": 3,"orange juice": 12,"bacon": 2,"tortilla chips": 8,"ramen": 20})
File "C:/Users/kyle/Desktop/yyyyyyyyyyyy.py", line 34, in grocery_cost
for n in input_value:
RuntimeError: dictionary changed size during iteration

silent nebula
#

Yes, you've modified the dictionary keys.
The standard fix is to take a copy of the keys and iterate over the copy.

for n in list(input_value):
#

This makes a list of the keys, and iterates over the list rather than the keys themselves (which you're modifying when you del input_value[n]).

lean parcel
#

it seems to be working now. thanks for the help

silent nebula
#

BTW, why this?

for i in stock:
                if n == i:

This will only inspect stock[i] where i==n, i.e. stock[i]. You don't need a loop here, just replace i with n and drop the for and if altogether .

lean parcel
#

i think i need it to make sure the keys are matched up

silent nebula
#

You don't. Just use n throughout. You've already checked that n is in stock, you're good to go.

lean parcel
#

so since n represents the key, i can just use the key as an index for stock as well?

silent nebula
#

Yes.

lean parcel
#

great, thanks

silent nebula
#

Rename it to eg input_key. Then it'll be really clear to you that you're accessing the stock with the input key.

lean parcel
#

!close

sick notchBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.