0

The following solution to the grocery problem in CS50P provided an output that was identical to the one requested in the CS50P Grocery List problem description. It works perfectly in all three Python IDEs that I have (using Linux). Yet when using check50 I get this error:

Results for cs50/problems/2022/python/grocery generated by check50 v3.3.6
:) grocery.py exists
:( input of EOF halts program
    expected exit code 0, not 1

This is my code (the indentation of "main" is wonky here but not in codespace). The error capture code is indistinguishable from a problem set where it worked without issue. I have spent an hour trying to find the issue and am at a dead end.

def main():
        try:
            itemlist=[]
            while True:
                item = input ("Your grocery item:  ").strip().upper()
                itemlist.append(item)
                itemlist.sort()
                itemdict = {i:itemlist.count(i) for i in itemlist}
        except EOFError:
            print()
            for key in itemdict:
                print(itemdict[key], key)
    main()

UPDATE: I installed VSCode Desktop and ran this code with the debugger on. According to VSCode there are no errors or issues with this code. Ctrl-d works as it should and the output is identical to what is shown in the problem instructions.

I am really hoping that a CS50 staff member is monitoring this forum. This problem is no solvable for a beginner!

3 Answers 3

3

Your program doesn't work when user choose to end the program via Ctrl-d without provide any input. When the user does so, your program jumps to the exception part of the code, which reference an undeclared name "itemdict". "itemdict" is never declared essentially because the try part never run.

On a side note, I don't think it's necessary to sort itemList after every append. It's more logical to append all the items then sort.

1
  • Thank you kbl. You are absolutely right. After taking the Univ. Michigan School of Info. course it became clear. That course is so much more in depth and detailed than the Harvard course. This was by far the most frustrating experience I have had taking online courses.. A person who has never programmed cannot easily take this course without spending many hours for each problem trying to find the correct python commands that were missing from the lecturer. A person with some previous experience might have no problem.
    – Mikech
    Commented Aug 10, 2022 at 15:51
1

Check50 will struggle to check as it still expects ctrl+d to stop the script in order to evaluate it further. Hence I would suggest you had a break at the end of your except EOFError:

-1

The CS50 check50 finally dropped the exception error if I put the try and except lines inside the while loop. I don't understand why that should be when 4 IDEs (including VScode - which is what CS50 uses) ran it without complaint the way it was. Moving the while loop required that I rewrite the code since that immediately started generating a host of logic errors, but I just dealt with each, one at a time, until I got it working again.

This course is not for absolute beginners! There is no detailed instruction on how these different things work, what can go wrong and why it can go wrong like I remember getting for Fortran 40 years ago. I get about 100 errors per program (sometimes more). Nothing works as expected. I spend 90% of my time searching the internet for detailed explanations of how the different commands work (at least 4 to 6 hours per program). I am very close to going somewhere else.

If this happens again where the output is error free and exactly as requested in CS50's preferred IDE (any every other IDE) and then Check50 won't accept it, that's it for me!!

But hey, its free. I would NEVER pay for a certificate!

2
  • I've ran into the same problem myself (Except that I am currently working on the Taqueria.py) this one is frustrating me to no end, cause as far as I can tell everything is running correctly, and with very little response from the community I am forced to go exploring for something that had no mention in the lectures (I would know, I've watched it 3 times now!) Unfortunately I feel like the python course is not as well fleshed out as the CS50x course given that it is relatively new. Sorry I can't help, but I share your frustration >XC Commented Aug 9, 2022 at 9:59
  • Thank you Gabby. I notice there are a lot of comments that the lectures do not provide the info needed to solve the problems. This is very true. I dropped the course and am now taking a course on Coursera with the University of Michigan. WHAT A DIFFERENCE! I cabn't go over the details in a comment. But do yourself a favor. Drop this course and go to UMSI. I can now easily do the Harvard problems and I am writing programs without errors. Those guys are not into glitz and glamour but they sure do know how to teach! AND EVERYTHING WORKS! Plus you are not forced to have a google account.
    – Mikech
    Commented Aug 10, 2022 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .