you're reading...

CS1: Lab Problem Set #0

CS1021C -001

Lab Problem Set 0

Professor Fred Annexstein


This problem set will introduce you to the programming environment (Canopy or IDLE) and to programming in Python, as well as to our general problem set structure. In this problem set, you will install Canopy or IDLE, write a set of simple Python programs, and hand them in. Be sure to read this problem set thoroughly, especially the sections of Collaboration and the Hand-in Procedure.


You may work with other students. However, each student should write up and hand in his or her assignment separately. Be sure to indicate with whom you have worked. For further detail, please review the collaboration policy as stated in the course information handout.

Installing Python and IDE

Follow the steps in Getting Python for installing Python, Canopy or IDLE onto the machine you plan to be using this term. Familiarize yourself with Python and the environment using the exercises given in the handout.

Note, when you first start using your system, make sure that the version number displayed is not 3.0 or higher. That version of Python is not backwards compatible with 2.7, which is the official Python version used in this course.

A Very Simple Program: Entering and Printing Your Name

The goal of this programming exercise is simply to get you more comfortable with using IDLE, and to begin using simple elements of Python. Standard elements of a program include the ability to print out results (using the print operation), the ability to read input from a user at the console (for example using the raw_input or input function), and the ability to store values in a variable, so that the program can access that value as needed.

Problem 1

Write a program that does the following in order:

Asks the user to enter his/her date of birth.

Asks the user to enter her/his city of birth

Asks the user to enter his/her last name.

Prints out the user’s last name,  date of birth, city of birth in that order.

An example of an interaction with your program is shown below (the words printed in blue are from the computer, based on your commands, the words in black are a user’s input – the colors are simply here to help you distinguish the two components):

Enter your city of birth:


Enter your city of birth:

**New York City

Enter your last name:


Annexstein was born on 01/31/1970 in New York City

Hints: To see how to use the print command, you may find it convenient to look at the input and output section of the Python Wikibook.


This will show you how to use print statements to print out values of strings.

To see how to read input from a user’s console into the Python environment, you may find it convenient to look at the same section (see for example the raw_input or input function).

Remember that if you want to hold onto a value, you need to store it in a variable (i.e., give it a name to which you can refer when you want that value). You may find it convenient to look at the variables and strings section of the Python Wikibook.

Problem 2: Exploring Turtle Graphics

Turtle graphics are an easy graphics package installed with Python. Although we will not spend much time on graphics in this course, working with turtle graphics provides a good exposure to a simple programmable graphics package. You are encouraged to try many examples available on the web to explore this package.

Step 1Create a Screen with a turtle on it.

Use the following code to create a graphics window with a turtle that draws a square. Feel free to comment out the code from Problem 1. This is most easily done by selecting that text and going to the ‘Edit’ option and selecting ‘Toggle Block Comment’.

import turtle         # import the turtle graphics module
scr = turtle.Screen() # create a window for the turtle
t = turtle.Turtle()   # create a turtle and place in middle
                      # code to move turtle t forward and right 4 times
for i in range(4):


Try running it and see if you get a blank canvas that pops up and draws a square. The final line of the program calls the mainloop() which allows the user to interact with the Turtle Screen. If you forget this line, you can’t close the window and go on with life. If you run in trouble with graphics window you can always reset python by going to ‘Run’ option and selecting ‘Restart Kernel’.

Step 2: Create a “constant” called SIDE_LENGTH

It is often useful to create a variable called a “constant” that has a value defined for it, but is not changed at all in the program. That value may be used over and over again, but instead of writing the value itself, you use the constant name for it.

For us, we need to define the length of the side of the house. Write this code in your file (at the top is fine):

SIDE_LENGTH = 150         # This is the length of a side.

It is also a great idea to make a comment for each constant, telling the reader what that constant is used for, as shown above.

Now use this constant in the program to draw a square with the indicated side length.

Step 3: Draw the roof of a house.

Make the turtle turn and draw the roof on top of the square. You’ll have to use a bit of geometry to figure out how long each roof part should be. The formula you need has something to do with the square root of 2. In order to compute the square root of 2, you’ll need to

  • Put import math at the top of the file.
  • Use math.sqrt(2) where you need it.sqare

Do not use any other numbers in your code (for distance travelled) except SIDE_LENGTH and formulas involving SIDE_LENGTH. (You may use 45 or 90 for turning a certain number of degrees.)

Step 4: Finish the drawing.

Write the code to finish the drawing of the house by placing an ‘X’ in the square connecting the opposite corners.

Extra Challenge: Organize the code so that the turtle does not redraw over existing lines (like the popular drawing challenge, and called an Euler Path in graph theory).

Submission Procedure

    1. Save your code in lab0.py. Do not ignore this step or save your file(s) with different names.
    2. Time and Collaboration Info

At the start of each file, in a comment, write down the number of hours (roughly) you spent on all the problems in that lab, and the names of the people you collaborated with.

For example:

# Lab Problem Set 0
# Name: Jane Lee
# Collaborators: John Doe
# Time Spent: 3:30
… your code goes here …

  1. Submit on BB


More Fun At Home With Turtles

Turtle Etch-a-Sketch

import turtle

turtle.setup(400,500)                # Determine the window size
wn = turtle.Screen()                 # Get a reference to the window
wn.title("Handling keypresses!")     # Change the window title
wn.bgcolor("lightgray")             # Set the background color
tess = turtle.Turtle()               # Create our favorite turtle

# The next four functions are our "event handlers".
def h1():

def h2():

def h3():

def h4():
    wn.bye()                        # Close down the turtle window

# These lines "wire up" keypresses to the handlers we've defined.
wn.onkey(h1, "Up")
wn.onkey(h2, "Left")
wn.onkey(h3, "Right")
wn.onkey(h4, "q")

# Now we need to tell the window to start listening for events,
# If any of the keys that we're monitoring is pressed, its
# handler will be called.




No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: