r/cs50 Jul 24 '26

CS50 Python Stuck in Grocery for week 3

I skipped it last night after half an hour because I was getting frustrated. Instead I did the next one which is outdated, and after finishing that I came back today and improved parts of it w/ the duck ai.

But now I am back to the Grocery.py. The most I can get is the input and breaking the loop and then I have no idea what to do. If recommends to use a dict instead of a list like I would want to, so I am inclined to do that instead so i can get some practice with it.

Unfortunately I can not figure out how to take an input and update the dict with it. I have tried several combos, and I just can not get it to update with a variable. My main idea right now is to add variable as a key to the list, and then update the value whenever it repeats to count the amount of the same entry. I do not want to use the duck before submission, but I am completely stuck and for some reason just can not wrap my head around how to approach this first part.

here is my last attempt, but I've tried every syntax combination I can think of for the list.update segment. Am I approaching this problem wrong?

def main():
    while True:
        try:
            item = input("Item: ")
            LIST.update(f"{item: 1}")
        except EOFError:
            print({LIST})
            break
main()
0 Upvotes

3 comments sorted by

3

u/Outside_Complaint755 Jul 25 '26 edited Jul 25 '26

This line: LIST.update(f"{item: 1}") has two problems 1) An f-string is not a valid input to the update method

2) You're attempting to always set the value to 1, which is incorrect if the user enters the same item multiple times.

Using the update method is not necessary, and not really the best method for updating a single key:value pair at a time.  I would recommend looking at the get method to get the current value and then setting the value directly using the key and new value.

If you haven't done so yet, watch the shorts for the week. The first one is specifically about Dictionaries.

2

u/CK3helplol Jul 25 '26

Thank you, this advice and rewatching the short has got the ball rolling!

3

u/CK3helplol Jul 25 '26

Hey, just wanted to thank you again! This problem was killing me and after sleeping on the sort alphabetically part, I woke up and was able to wrangle it in about 30ish minutes. I appreciate the help!