Reading Code#

Warmup Exercises#

Exercise 40 (Ice cream flavors)

Make a list of ice cream flavors, then print a menu with a number next to each flavor.

Flavors: Banana, Chocolate, Lemon, Pistachio, Raspberry, Strawberry, Vanilla

Exercise 41 (Ice cream combinations)

Print every possible sorbet duos from the same list of flavors. Don’t print recipes with twice the same flavor (no “Chocolate Chocolate”), and don’t print twice the same recipe (if you print “Vanilla Chocolate”, don’t print “Chocolate Vanilla”, or vice-versa).

Flavors: Banana, Chocolate, Lemon, Pistachio, Raspberry, Strawberry, Vanilla

Intro#

We spend most of our time learning to write code, but just as important is knowing how to read and understand code. So for this exercise we’re going to practice reading code.

We’ll step through a small code sample, similar to how we did in in our Program Flow lesson. For each line that we walk through, you’ll see a section that looks like this. (Labels in blue are just for this example.)

1level = 1
2name = "Yoshi"
3
4print("Starting level", level)
5print("Welcome", name)

↑ Code sample


Each breakdown has 5 sections:

  • code sample (on the left) – shows the code with the current line highlighted

  • variables (1st on the right) – shows the variables that exist at the time when the line is evaluated

  • steps to evaluate (2nd on the right) – shows the step by step process of evaluating the code

  • output (3rd on the right) – shows the output of the code, with the current line highlighted

The above example is a breakdown of line 5.

Reading a while loop#

Let’s walk through a simple while loop.

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

1total = 0
2i = 0
3while i < 3:
4    print("loop", i)
5    total = total + i
6    i = i + 1
7print("The total is:", total)

Exercises#

Exercise 42

In the following code sample, what is the value of name on line 12?

 1characters = [
 2    "mario",
 3    "luigi",
 4    "yoshi",
 5    "bowser"
 6]
 7
 8i = 0
 9
10while i < len(characters):
11    name = characters[i].upper()
12    print(i+1, name)
13    i += 1