1

I took CS50's Introduction to Programming with Python recently and I'm stuck at the CS50P-Shirt problem from pset 6. Though the output is correct, the program fails to exit with exit code '0' (according to check50). Here's my code for the question;

import sys, os
from PIL import Image, ImageOps

def main():
    cmd_ln_arg()

    shirt = Image.open('cs50_Tshirt.png')
    picture = Image.open(sys.argv[1])
    picture = ImageOps.fit(picture, shirt.size)
    picture.paste(shirt, shirt)
    picture.save(sys.argv[2])

def cmd_ln_arg():
    if len(sys.argv) < 3: sys.exit('Too few command line arguments!')
    if len(sys.argv) > 3: sys.exit('Too many command line arguments!')
    ext1 = os.path.splitext(sys.argv[1])[1]
    ext2 = os.path.splitext(sys.argv[2])[1]
    if ext1 != ext2: sys.exit('Different extensions are not accepted!')
    if ext1 not in ['.jpg', '.jpeg', '.png']: sys.exit('Invalid Extensions!')
    if not os.path.exists(sys.argv[1]): sys.exit('File not found!')

if __name__=='__main__':
    main()

I know that exit code 0 means the program runs successfully and exit code 1 means the program is faulty. Where best can I know more about them, is it possible to know the reason for the program exiting with a certain exit code? Or am I missing something else?

and the check50 results here; enter image description here

1 Answer 1

2

You have the right idea -- exit codes provide feedback on success/failure in a program (or function). Exit code 0 represents success, and any positive number means an execution error was detected. (Different errors can generate different error numbers.) In your case, I don't think the error is caused by a coding error (per se). Your program is reading the shirt image from a file named 'cs50_Tshirt.png'. When I did this project (5 months ago), the file was named 'shirt.png'. Likely the automated test script is testing with a file named 'shirt.png' and fails when it tries to read 'cs50_Tshirt.png'.

Here's how to confirm the behavior. Rename your 'cs50_Tshirt.png' file to 'tshirt.png', and run your program. I expect you will get an error with a traceback.

pset_6/shirt/ $ python shirt.py before1.jpg pasted1.jpg
Traceback (most recent call last):
  File "/workspaces/68927141/pset_6/shirt/shirt.py", line 23, in <module>
    main()
  File "/workspaces/68927141/pset_6/shirt/shirt.py", line 7, in main
    shirt = Image.open('cs50_Tshirt.png')
  File "/usr/local/lib/python3.10/site-packages/PIL/Image.py", line 3068, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'cs50_Tshirt.png'

To get the exit code (on Linux), enter this on the command line (immediately after you run your program):

pset_6/shirt/ $ echo $?
1   <-- this is the value of the error code referenced as $?

To get the exit code (on Windows), enter this on the command line (immediately after you run your program):

E:[.\shirt]->echo %ErrorLevel%
1   <-- this is the value of the error code referenced as %ErrorLevel%

Change the name of the shirt file and in your program, and see what happens. Good luck.

4
  • GREAT! It worked, but then, is it possible to know exactly which line caused such an exit code and why?
    – Bharath V
    Oct 31, 2022 at 7:05
  • And could this mean check50 isn't using Regular Expressions?
    – Bharath V
    Oct 31, 2022 at 7:07
  • True, I doubt check50 is using regular expressions -- they aren't part of this pset. The input shirt filename is NOT provided as a command line argument. It is hardcoded . So an error occurs on this line: Image.open('cs50_Tshirt.png') when the program is run with a file named 'shirt.png'. I will update my answer to show how you can get the exit code.
    – kcw78
    Oct 31, 2022 at 12:59
  • Regarding which line...you can get this with try/except around the Image.open() functions. Remember those from Lecture 3? :-) If you add this, the program will raise an exception and give the line number when it can't open image the file.
    – kcw78
    Oct 31, 2022 at 13:01

You must log in to answer this question.

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