#๐ comparing dictionaries then updating based on quantities
62 messages ยท Page 1 of 1 (latest)
@lean parcel
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.
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)
Error messages? Output? What is the type of input_value?
type of input is dictionary
You should also use more descriptive names than n and i. Like stock_key for i.
A dictionary mapping what to what?
when i enter no instead of yes for input within the function, i'm getting an UnboundLocalError
!traceback
Please post to full output and 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.
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
This is not a full traceback.
See the info box above.
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
Is that the current code? because it doesn't look like n can be undefined at that line.
So it maps stock keys to order quantity?
Also the code you cited doesn't even have 128 lines.
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
So? Just post the whole file.
stock and order keys match up and compare quantities
why
So we can match up your traceback line numbers with the correct code.
Ok. it's always good to reduce a problem to the smallest thing you can which shows the problem.
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
Cool. And post yyyyyyyyyyyy.py ?
it's the same right?
I don't know that! I want to see the actualy code which caused the traceback above.
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)
del n
This is very strange. You almost never want to delete a name. Did you intend to delete from a list there?
i'm trying to delete both the key and value from input_value dictionary
But right after the if you use both n and input_value[n]
input_value[n] is the value not the key right?
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).
I suspect the OP wanted to del input_value[n], but shouldn't be trying to adjust the quantity if they do so.
Ya, whoops, had the wrong dictionary name.
del input_value[n] will only be deleting the value or it will delete both value and key?
It deletes the entry with key n, so that deletes the key and its value.
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)
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.
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
If you del the input value, go continue to do the next loop iteration, skipping the stuff below.
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
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]).
it seems to be working now. thanks for the help
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 .
i think i need it to make sure the keys are matched up
You don't. Just use n throughout. You've already checked that n is in stock, you're good to go.
so since n represents the key, i can just use the key as an index for stock as well?
Yes.
great, thanks
Rename it to eg input_key. Then it'll be really clear to you that you're accessing the stock with the input key.
!close
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.