15110 Summer 2015

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:

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

Part 2 - Loops with Lists

2.1 Class Activity

2.2 Student Activity - write a loop over a list

In the file loop_with_list.py:

Part 3 - Nested Loops With Lists

3.1 CA Demonstration - nested loop control flow

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: