The purpose of today's lab is to give you familiarity with the systems we will be using throughout the semester. There are 3 components:
Read the instructions below carefully and complete each tutorial and exercise as indicated. In this course you will often work as part of a pair, but for today, please work on your own (asking lots of questions of those around you!)
Please keep the following things in mind as you do the lab exercises in this course:
To design and implement algorithms on a computer, you must first know how to control the basic operations of the computer. This is one of the fundamental purposes of the operating system. Study the tutorial appropriate for the operating system that you will be using in these labs (only!). It is important for craftspeople to know their tools well, so we suggest that you at least review the material even if you’ve used the system in the past.
While it is possible to write programs using only simple text editors, it is often preferable to work within a system specifically designed for writing code. This is the purpose of an IDE. IDEs usually provide source code editors (including some built-in features making it easier to spot syntax errors), the ability to run your code as a program, and a means of helping to debug your program.
In this course we will be using the Eclipse IDE, together with a plug-in (that is, an extension of Eclipse) called PyDev which can be used for Python code development.
Using what you have learned in the Operating System (Linux, Windows or Mac) and Eclipse introductions you should now be able to complete the following exercise:
Do the following things on your development system:
cs108and a folder inside that project called
catcommand from the terminal window to check the file’s contents).
Python is a powerful programming language with many built in features and commands. For example, Python gives the programmer the ability to evaulate mathematical expressions, receive input from the user, and print values to the console window with basic commands.
More advanced features are also available by making use of previously
created modules, which can be thought of as
collections of code. Today, we will use a module called
which gives us access to a collection of functions for Turtle
Create a new file in your lab01 folder called
. NOTE: Rules for Python file names:
Double check that you named the file correctly. If you did not, right-click on the name of the file and choose Rename... to correct your file name.
Copy and paste the following Python code into your file:
''' A first turtle graphics program Created Fall 2014 Lab 01 @author: Serita Nelesen (smn4) ''' # Gain access to the collection of code named "turtle". import turtle # Give the name "window" to the screen where the turtle will appear. window = turtle.Screen() # Create a turtle and name it bob. bob = turtle.Turtle() # Tell bob the turtle to move forward 250 pixels. bob.forward(250) # Keep the window open until it is clicked. window.exitonclick()
Click the green arrow button (known as "Run"). You may have to specify a Python Run.
Watch the "turtle" draw a line across the screen.
Click anywhere on the screen to close the window.
Take a minute to study this code. At the top, contained within triple quotes is a “docString”. Every file you ever submit *must* have such a docstring that includes the following features:
The lines beginning with # are
, that is, information ignored by Python but useful for the
programmer. Good code is always carefully documented. By convention,
comments describe the code that follows.
In the code above we tell Bob the turtle to move forward, but there are other commands we can give Bob:
bob.backward(distance): have the turtle move backward distance pixels
bob.right(angle): have the turtle turn right by angle degrees
bob.left(angle): have the turtle turn left by angle degrees
bob.penup(): have the turtle pull the pen up (i.e. stop drawing as it moves)
bob.pendown(): have the turtle put the pen back down (i.e. start drawing as it moves)
Let's extend the sample code to have the turtle draw a star instead of the current line. You can do this as follows:
Update the doc string at the top of the file to indicate that this program will draw a star.
Add your name and login id to the list of authors. You can do this
by adding another line below the current authors:
@author: Your name (your login id)
Give the turtle commands that implement the following algorithm.
Hint: How many degrees are there in a circle? And how many points in the star?
When your turtle correctly draws a star, make sure to save your program. Your file is probably is already saved, but it never hurts to check. You can tell if a file is saved by looking for an asterisk (*) next to the name of the file in the tab at the top of the screen. If there is no asterisk, the file has already been saved.
There are many more commands that you can give a turtle. These are described in the Application Programmer Interface (API) for the module: Turtle graphics. An API gives the names of commands that are available in the module, as well as information about what inputs are required and what outputs can be expected. The API for all of Python is available here: Python documentation
Right now, this documentation is probably overwhelming, but as we proceed through the course you will learn how to use this resource to find helpful modules, and then how to use those modules effectively.
Use what you have learned to have the turtle draw the picture on the
right. Create a new file for this program called
and include appropriate header documentation. Then implement an
appropriate algorithm, such as the one given here.
The steps of the algorithm should appear as comments in your code to help keep track of what is happening where.
We will grade these exercises according to the following criteria:
Submit your solutions to these lab exercises using the appropriate tool.
If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!