# 15110 Summer 2017

## Lab 3

### Goals

The purpose of this lab is to get practice with loops (for loops and while loops) that we covered in class. In addition, you will be using the list datatype of Python. After completing the tasks in this lab, you should be able to do the following:
• Given a conditional, for loop or a while loop, determine how the control will flow (what instructions will be executed, how many times)
• Write for loops and while loops
• Create a list with given elements. For example, name_list = ["John", "Mary", "Jane"].
• Given a list and an index, access the list element at the given index. For example, name_list.
• Given a list, obtain its length. For example, len(name_list).
• Iterate over a list using a for loop or a while loop.
• Write nested for and while loops (loops within loops).

### Deliverables

1. for_loop.py
2. loop_with_list.py
3. nested_loops.py

Place the files in a lab3 folder. Before leaving lab, zip up the lab3 folder, and hand the zip file in.

## Part 1 - Loops

### 1.1 CA Demonstration - explanation of the following code

The following segments of code all do the same thing, but they can be done in different ways.

No loop For Loop While Loop

print("hello")
print("hello")
print("hello")
print("hello")
print("hello")


for i in range(0,5):
print("hello")
OR
for var in range(0,5):
print("hello")


i = 0
while(i < 5):
print("hello")
i = i + 1


### 1.2 Student Activity - write a loop function

• In the file for_loop.py, define a function print_n_lte_even(n) that will print all the non-negative even numbers less than or equal to n. Your function must use a for loop to get full credit.
          Sample Output:

>>> print_n_lte_even(5)
0
2
4
>>> print_n_lte_even(6)
0
2
4
6


## Part 2 - Loops with Lists

### 2.1 Class Activity

• Creating a list, accessing an element, updating an element
    mylist = list()
mylist = []
mylist = ["15110", "is", "so","much", "fun"]
mylist
mylist
mylist   # Error!
mylist = "Computer Science"

• Looping Over Lists

Loop using List Index Loop using List Element
  mylist = ["a", "b", "c", "d", "e", "f"]
word = ""
for list_index in range(0, len(mylist)):
word = word + mylist[list_index]

  mylist = ["a", "b", "c", "d", "e", "f"]
word = ""
for list_element in mylist:
word = word + list_element


### 2.2 Student Activity - write a loop over a list

In the file loop_with_list.py:

• Define the function build_list_string(string_list).
• The function should use a for loop that indexes into a list of arbitrary size.
• The function should return the string of all the elements in the list concatonated together.
• You can test your function with the following lists.
•             listA = ["1","5","1","1","0"]
listB = ["x", "y", "z"]

Sample Output:

>>>build_list_string(listA)
'15110'
>>>build_list_string(listB)
'xyz'


## Part 3 - Nested Loops With Lists

### 3.1 CA Demonstration - nested loop control flow

• Nested for loop example:
   for number in range(1,4):
for letter in ['a','b','c']:
print(number, letter)


### 3.2 Student Activity - sundaes and triangles

1. Please place solutions to the following two problems in nested_loops.py: Suppose we are making ice cream sundaes. We have four flavors of ice cream: vanilla, chocolate, strawberry, and pistacchio. And we have three sauces: caramel, butterscotch, and chocolate. How many different ice cream sundaes can we make? In the file nested_loops.py define a function sundaes() to systematically print out every possible combination, one per line. For example, the first line should say "vanilla ice cream sundae with caramel sauce". You should create a list to hold each class of ingredient, and use nested for loops to iterate over these lists to generate the combinations. At the end, your function should return an integer giving the total number of combinations.

To get you started here a few things you will need:

          flavors = ["vanilla", "chocolate", "strawberry", "pistacchio"]
sauces  = ["caramel", "butterscotch", "chocolate"]

print(flavor + " ice cream sundae with " + sauce + " sauce")


2. (optional - give it a try if you have time) Consider the following output as shown:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55


Complete the missing parts so that it creates the output above. Note that the columns of numbers do not need to line up perfectly. Run your program in using python3 to test your work. HINT: Row i has i columns.

def triangle():
value = 1
row = 1
while row <= _____:
column = 1
while column <= _______:
if column != _______:
print(value, ' ', sep = '', end = '')
else:
print(value)
value = value + 1
column = column + 1
row = row + 1

3. (Optional) if you have time consider the following:

• See the lecture notes of today and use shell to apply the statements and functions explained about lists.
• See the practice questions in piazza and select any question and write programs as you wish.