Lab 2
Due: Friday, Feb. 10 at 11:59pm on Moodle
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
range( )
functionMake 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.
Write a for-loop using range( )
that prints out three lines in this order:
Make sure you understand how to write for-loops that loop over explicit lists. Test out all the examples below:
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.
Make sure you understand how to convert between different data types, and also query the type of a variable. Try out the following commands:
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.
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.
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) BrownYou 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!
Now explore using two lists in one for-loop:
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:
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.
Note: you can achieve a quote character inside a string by using single quotes: i.e. '"'
.
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: