CSC 111: Intro to Computer Science through Programming

Lab 2

Due: Friday, Feb. 10 at 11:59pm on Moodle

The goal of this lab is to review several key aspects of Python (in parts 1-3), and learn a few new aspects (in parts 4-5). In the last part you will have a chance to put all these pieces together. For this lab, copy over all your work from the shell into a transcript file (you can copy it over in parts if you end up closing and opening IDLE). You will submit all the shell content at the end. For all parts besides the last one, you can work in the shell, not a separate file.

For this lab, first find your randomly assigned partner. Introduce yourselves - the person with the first name that comes last alphabetically should begin as the "driver", with the other partner as the "navigator". The driver will have the code open, and the navigator will have these instructions open.

During this lab we will be switching more frequently. At the end of the lab, email the code and transcript to the person who started as the navigator. Both partners should submit the same code on Moodle.

Credit: modified from materials by Joe O'Rourke

1. Review: range( ) function

Make sure you understand the range( ) function. Test out all of the examples below. Type them into the shell---no need to create a new file.

range
Extension Question:

Write a for-loop using range( ) that prints out three lines in this order:

  • 13
  • 10
  • 7

Switch Driver and Navigator here!

2. Review: Loops over lists

Make sure you understand how to write for-loops that loop over explicit lists. Test out all the examples below:

lists

Extension Question:

Write a for-loop running over a single list ['one', 'two', 'three'] that prints out

one 1
two 2
three 3

You will need a separate variable (say, n) to hold the 1, 2, 3, updated in the loop, and print that out.

Switch Driver and Navigator here!

3. Review: Types

Make sure you understand how to convert between different data types, and also query the type of a variable. Try out the following commands:

types

Extension Question:

Write a for-loop that prints the following output:

.1
..2
...3
....4
.....5
......6
.......7
........8

Hint: your code should cast the integer so it can be concatenated with the periods.

Switch Driver and Navigator here!

4. New: Indexing into lists

Depending on how far we get on Wednesday in class, we might not have seen this topic yet, but I think it can be learned and understood through working interactively with lists.

Explore until you understand how indexing into lists works. The index is a number (an int) that selects one element of a list. The index is placed between square brackets: e.g., animals[2]. But note the index starts at zero (like range starts at 0 by default). The first element of a list has index 0. Perhaps it should be called the "0-th" element.

One can select an element with an explicit number, such as animals[2], or with a variable (usually a loop variable), such as animals[k]. Below, create a list of animals, and the explore indexing into this list. Also note that we can use the built-in function len( ) to obtain the length of a list, just like we obtained the length of a string.

indexing

Extension Question:

Consider the list of colors (from a 12 pack of Crayola crayons) below:


colors = ["Red", "Red Orange", "Orange", "Yellow", "Yellow Green","Green",
          "Sky Blue","Blue","Violet (Purple)","White","Brown","Black"]
Write a for-loop to produce the following output (every other color starting with red):

Red
Orange
Yellow Green
Sky Blue
Violet (Purple)
Brown
You can copy and paste the list of colors into the shell, no need to type it out. Note: there is more than one way to do this part!

Switch Driver and Navigator here!

5. New: Index into two lists

Now explore using two lists in one for-loop:

animals
Extension Question: Consider the two lists below (one for colors, and one for their corresponding hex codes):

colors = ["Red", "Red Orange", "Orange", "Yellow", "Yellow Green","Green",
          "Sky Blue","Blue","Violet (Purple)","White","Brown","Black"]

hex_codes = ["#ED0A3F","#FF3F34","#FF861F","#FBE870","#C5E17A","#01A368",
             "#76D7EA","#0066FF","#8359A3","#FFFFFF","#AF593E","#000000"]
Using a for loop, create the output shown below:

colors

Switch Driver and Navigator here!

6. Clapping Land Song

Your task here is to print out five verses of this Scandinavian song exactly. This requires you to use strings, lists, loops, indexing into lists, and print( )'s. If you've completed the above exercises, you should have all the pieces to write this program.

Each verse of the song is largely the same, except for one verb, and one gerund derived from that verb. (A gerund is a noun form of a verb produced by adding -ing.) So the first verse uses clap and clapping, ..., the fifth verse uses jump and jumping. All the remainder is identical. It is much too difficult to derive the gerund from the verb, because sometimes the consonant is doubled (clapping) and sometimes not (jumping). So I recommend that you create two lists: one list for the verbs in the order in which they appear in the song, and one list for the gerund forms of those verbs, in the same order. You should be using loops - it is not an option to just print the entire song verbatim!

For this part, create a file called lab2.py with a main method, so that when it is run, the complete song below is printed. This is the file you will submit for lab.

ClappingLand

Note: you can achieve a quote character inside a string by using single quotes: i.e. '"'.

Transcript and Submit

From the shell, highlight all the testing you have done and all the output (since the beginning of lab) and copy it into a plain text file (.txt extension). On Windows you can use the program Notepad (under Accessories) and on Mac you can use the program TextEdit (under Applications). For Mac, if it doesn't have an option to save as txt, go to "Format" -> "Make Plain Text", then save again. Save this file as lab2_transcript.txt.

Make sure that both you and your partner have a copy of all the code written during this lab, and the transcript. Both partners should submit the same files:

  • lab2.py

  • lab2_transcript.txt
on Moodle. If you did not finish, you can either meet to finish or finish separately. All labs must be submitted by Friday night. If you finish early, I encourage you to start on Homework 2, but you are also welcome to depart.