Turtle

Welcome to Making with Code! We're glad you're here! yay πŸ˜„

Into the Terminal

Take a peek at your Desktop. Assuming you already completed the initial setup, you should see a new folder called making_with_code. We are going to navigate to that folder using the Terminal interface.

Terminal: a new user interface

You're probably used to interacting with the files in your computer through a Graphical User Interface (GUI) like Finder. Terminal allows us to interact with the files in our computer through a Text-based User Interface (TUI). The files in our computers are organized in nested folders known as directories.

Open a new Terminal window. Terminal opens in your home directory (also known as ~), but we will be working in the making_with_code directory. Since the home directory holds all your stuff, the making_with_code directory must be somewhere inside the home directory.

πŸ’» Type ls into the command line and press return. This will list all the files and subdirectories in the current directory. You should see that Desktop is one of the subdirectories listed. Let's move into that subdirectory.

~$ ls
Applications  Desktop  Documents  Downloads	 Library  Movies  Music	 Pictures

πŸ’» Type cd Desktop into the command line and press return. cd stands for "change directory". Now, list all the items in your Desktop directory using ls.

~$ cd Desktop
~/Desktop$ ls
Screen Shot 2019-08-15 at 12.34.48 AM.png  dobby.gif			  warsaw-boarding-pass.pdf
making_with_code						                   lentil loaf gravy.pdf

Compare the output in the Terminal window with the Desktop shown by the GUI.

πŸ’» Type open . to open Finder. All of the files and folders are the same! The Terminal shows us the same files and directories as our GUI.

Comparing Terminal with the GUI

Going back to the Terminal, we can also see that the making_with_code subdirectory is inside the Desktop directory.

πŸ’» Change into the making_with_code directory and list what it contains. There is a subdirectory called mwc1 and another subdirectory that named unit1, which holds everything related to Unit 1. Inside unit1 is yet another subdirectory, lab_turtle, which is where we want to be today.

~/Desktop$ cd making_with_code
~/Desktop/making_with_code$ ls
mwc1
~/Desktop/making_with_code$ cd mwc1
~/Desktop/making_with_code/mwc1$ ls
unit1
~/Desktop/making_with_code/mwc1$ cd unit1
~/Desktop/making_with_code/mwc1/unit1$ ls
lab_turtle
~/Desktop/making_with_code/mwc1/unit1$ cd lab_turtle
~/Desktop/making_with_code/mwc1/unit1/lab_turtle$

Introduction to writing code

Now that you can navigate in the Terminal, let's write some code! Throughout the class, we will be using the Python programming language to help us perform computational tasks. In this unit, we'll be using a software library called turtle to draw things with code.

Every time you start working on a project, you need to enter a shell which is configured properly for that project using a tool called Poetry.

πŸ’» Type poetry shell. You can tell you're in the right shell because the prompt starts with (lab-turtle-py3.11). You can exit this shell by typing exit.

MWC/modules/lab_turtle % poetry shell
(lab-turtle-py3.11) MWC/modules/lab_turtle %

Writing programs

Python programs start out as simple text files. To write a Python program, we start out by writing a text file. During the setup, we downloaded a special text editor made for the purpose of writing code. Before you start, make sure you are still in ~/Desktop/making_with_code/mwc1/unit1/lab_turtle.

πŸ’» code drawing.py

This should open a new VS Code window with a tab that says drawing.py. Python programs consist of lines of code that tell your computer what you want it to do. Read the program in drawing.py. Can you guess what it's going to draw?

from turtle import (
    forward,
    back,
    left,
    right,
    penup,
    pendown,
    color,
)

forward(100)
right(360 * 2 / 5)
forward(100)
right(360 * 2 / 5)
forward(100)
right(360 * 2 / 5)
forward(100)
right(360 * 2 / 5)
forward(100)
right(360 * 2 / 5)

input()

Running programs

Let's run drawing.py to see what it does.

πŸ’» Run python drawing.py in Terminal. Press enter in the Terminal when you are done.

You just ran your first Python program! Congrats!! πŸŽ‰

Before we move on, here a summary of the commands you just learned, plus a few extras.

CommandDescription
cd Desktop/making_with_code/mwc1/unit1to change to the directory "unit1"
code .to open VS Code.
code newfilename.pyto make a new file. You can also choose to make a new file by right-clicking on the folder in VS Code.
python newfilename.pyto run the program.
↑to get to the previous command you typed in terminal
<tab>autocompletes the command or path as much as possible
<tab> <tab>shows possible autocompletions

Let's draw!

Now that you've got the basics, try to make it more interesting.

πŸ’» Experiment with turtle commands below by editing your drawing.py file. After you make some changes, save the file in VS Code and then run it in Terminal to see the changes.

FunctionInputExample UseExplanation
forwardamountforward(100)Moves the turtle forward by the specified amount
backamountbackward(100)Moves the turtle backward by the specified amount
leftangle in degressleft(45)Turns the turtle counter clockwise by the specified angle
rightangle in degreesright(45)Turns the turtle clockwise by the specified angle
penupNonepenup()Picks up the turtle/pen so that it doesn’t draw when it moves
pendownNonependown()Puts down the turtle/pen so that it draws when it moves
colorcolornamecolor("red")Sets the color for drawing. Use "red", "black", etc. Here's a list of all the colors.

Error and bugs

While trying this out, you may come across errors or bugs, do not fear! Write the issue down, and we can talk about it during class. Try to figure out whether your bug is a:

Programmatic error (which happens when the program does something different than what you wanted. Remember that this happened when we set the angle to the wrong number)

Syntax error (which happens when the program crashes because the program cannot be understood by the computer. Remember that this happened when we forgot a parentheses).