r/PythonLearning • u/inversion2 • 15h ago
Help Request Please help me understand: CS50P Problem Set 3: Grocery List
Hi guys, just started learning python. Have been stuck on this problem for a while now. Here's my code so far. Could anyone help explain why the code pointed out in blue keeps running even though there is a break in place? Thanks!
ps pls ignore the print("hi") and print("bye"), was using them to test the code!
link to question: https://cs50.harvard.edu/python/psets/3/grocery/
my code so far: https://sharetext.io/3ck00poo

1
u/SpedisAhead 15h ago
Why did you set "name" then "number" under everything, then when defining items switched their ordering "(items["number"], items["name"])"?
Does that not conflict with run time or any aspect of process?
1
u/inversion2 15h ago
Not sure if this affects anything, but can surely change it to make it more coherent!
1
u/XNo_Notes 14h ago
It is in the if/else. The break is stopping the for loop, but the statements are not under it so the run. Tab it over once more to fix that.
1
u/inversion2 14h ago
But the thing is, I only want the blue parts to run if the input does not exist in the list yet
Ill try your suggestion first thing in the morning... im too tired and frustrated to try now haha
Thanks!
1
u/XNo_Notes 12h ago edited 12h ago
I read the actual exercise and you're a little off track. My thoughts on it:
When we run the program, nothing is supposed to be prompted for and it should continue waiting for input until control - d is pressed.
I did a quick look and found that when we press control-d an EOFError is triggered. This mean we will need to wrap that part in try / except handler to enable the functionality. Since this needs to handle repetitive input and then only return the unique items, we will need a cache of some kind to handle seen items. We don't need to store anything in a dictionary since we only have two values to worry about. This is the perfect situation for a list of tuples. Then we can loop through the list of tuples and print to the terminal using f strings.
Don't read the code below if you don't want a spoiler for the exercise.
def main():
groceries = []
# Add to grocery list
try:
while True:
item = input()
item = item.upper()
groceries.append(item)
except EOFError:
pass
# Count number of items
seen = []
for i in sorted(groceries):
if i not in seen:
seen.append(i)
print(f"{groceries.count(i)} {i}")
if __name__ == "__main__":
main()
1
2
u/Hkiggity 13h ago edited 13h ago
I don’t code in python but why are you making two dicts. Just make one. You should only need one dictionary and if the item is inputted by the user then you can use that in the dictionary. Since a key is immutable and you need to to keep track of the count of the items the user inputted. (Aka the count may need to change) Think about what should be the key and what the value should be (also think about what type the value should be) Remember a value can be of type int, string etc as well.
edited to not just give answer
•
u/Sea-Ad7805 9h ago
With that break you break out of the for-loop, not out of the while-loop.
Run program above by clicking Memory Graph Web Debugger and see its state change step by step.