Code as a Liberal Art, Spring 2024

Unit 1, Lesson 1 Homework

Due: Wednesday, January 31, 8pm (Date error corrected. Sorry for any mixup.)

Experiments with running Python files

  1. Before beginning the homework for this week, make sure to read through Unit 1, Lesson 1 for explanation of all that you should need to complete the below exercises. We went through parts of this lesson rather quickly in class and will start class next week with a review.

  2. Download this file: unit1-lesson1.zip. Double-click on it to unzip it, and move the folder named Week 0 part B into the code folder that you have created for all of your work for this class this semester.

    Open VS Code, and drag this new folder into the VS Code window. Click "trust" if prompted. You should now see something like this:

  3. (Click to enlarge.)
  4. Run a simple Python program that generates output on the command line.

    Click on command_line_output.py. Examine the code if you would like. Click the play button and you should see something like this:
    (Click to enlarge.)
    You have just run a Python program and observed its output on the command line. Take a screenshot. Make a new folder in Google Drive called "Week 0, Part B" and move the screenshot into the folder.

    Can you modify this program at all to change its output? Hint: try changing the numbers 0, 10, or 1. After a change, click Play again. If you click play and your program will not stop running, you've probably created an infinite loop. Click the trashcan icon to stop it:

    (Click to enlarge.)

    Can you modify this very simple algorithm so that it only prints even numbers? What about odd numbers? What about negative numbers from -1 to -9?

    If you were able to generate some different output, take a screenshot and copy it to your Google Drive folder.

  5. Run a Python program that requires user input on the command line.

    Next, click on guessing_game.py, examine the code, and again click the play button. This runs an interactive Python program that will require your input to proceed. Click anywhere in the Terminal window, and type on your keyboard to follow the instructions. Take another screenshot and copy it into your Google Drive folder.

  6. Run a simple Python program that opens a file and reads that file as input data.

    Next, click on business_card_read.py and examine the code. What do you think this will do? Click play to run the program. It prompts you for a file name. Look in the "Explorer" area of VS Code. There are two .txt files there: rory.txt and gritty.txt. In response to the prompt, try typing one name, pressing enter, and seeing what happens. You need to make sure to enter the name precisely with no mistakes. Now run the program again and try entering the other name. Take a screenshot of the two successful runs together and put that in Google Drive.

  7. Run a simple Python program that creates a new file of output data in the same folder.

    Building on that, next click on business_card_create.py, examine the code, and press play. This program will prompt you for some input, and then will create a new .txt file. What is this file called? Click on this new .txt file to examine its contents. Take a screenshot of the command line output with the new file and upload this to Google Drive.

  8. Run a Python program that creates a new image file in the same folder using the Python Image Library.

    Finally, click on image_create.py and examine its contents. Click play. Again this will prompt you for some inputs and will create a new file, but instead of creating a plain text (.txt) file, it creates a PNG image file (.png). Try running this multiple times and experimenting with multiple color values.

    What happens if you type an invalid color? What type of code could you imagine adding to prevent this error message?

  9. Now that you have successfully completed all these experiments with running Python programs using the Terminal via VS Code, return to Finder (Mac) or Explorer (Windows) and again examine the folder that you unzipped. Inside this folder you should now see all the new files that you have created. Take a screenshot of this and upload it to Google Drive.

Finish homework 0

If you have not yet completed Homework 0, including reading the Help page, creating a Gist, and sharing it with me, please make sure to do that now.