Skip to content

Commit e8f2f67

Browse files
author
Philip Guo
committed
woo
1 parent 3720526 commit e8f2f67

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

v3/js/opt-lessons.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ function updateLessonNarration(myViz) {
101101
$(document).ready(function() {
102102

103103
//$.get("lessons/aliasing.txt", parseLessonFile);
104-
$.get("lessons/dive-into-python-311.txt", parseLessonFile);
104+
//$.get("lessons/dive-into-python-311.txt", parseLessonFile);
105+
$.get("lessons/for-else.txt", parseLessonFile);
105106

106107
// log a generic AJAX error handler
107108
$(document).ajaxError(function() {

v3/lessons/for-else.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
primes = []
2+
for n in range(2, 10):
3+
x_range = range(2, n)
4+
for x in x_range:
5+
if n % x == 0:
6+
break
7+
else:
8+
# loop fell through without finding a factor
9+
primes.append(n)
10+
======
11+
{
12+
"title": "For-Else Construct",
13+
14+
"description": "Loop statements may have an <code>else</code> clause; it is executed when the loop terminates through exhaustion of the list (with <code>for</code>) or when the condition becomes false (with <code>while</code>), but not when the loop is terminated by a <code>break</code> statement. This is exemplified by the following loop, which searches for prime numbers. (Yes, this is the correct code. Look closely: the else clause belongs to the for loop, not the if statement.) <small>[Source: <a href='http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops'>Python.org tutorial</a>]</small>",
15+
16+
"1": "First initialize a list to hold the prime numbers found between 2 and 9 (inclusive).",
17+
18+
"4": "Note that this inner <code>for</code> loop has an associated <code>else</code> clause on line 7.",
19+
20+
"5": "The first iteration of this <code>for</code> loop immediately exits and jumps to the <code>else</code> clause because <code>range(2, 2)</code> is empty.",
21+
22+
"7": "Test whether <code>n</code> is divisible by <code>x</code> to determine primality ...",
23+
24+
"11": "Since the loop exits again without running a <code>break</code> statement, the <code>else</code> clause is executed.",
25+
26+
"16": "4 is divisible by 2, so it's not prime. The inner <code>for</code> loop will exit by running the <code>break</code> statement, so its associated <code>else</code> clause will <i>not</i> be executed.",
27+
28+
"19": "The program now tests whether 5 is divisible by 2, 3, or 4.",
29+
30+
"26": "Since 5 is prime, the inner <code>for</code> again exits normally, so its associated <code>else</code> clause will be executed.",
31+
32+
"59": "When the program terminates, <code>primes</code> contains the list of primes found between 2 and 9 (inclusive)"
33+
34+
}

0 commit comments

Comments
 (0)