Skip to content

Commit ee226c3

Browse files
committed
Tweaks to the nested list as a book
1 parent a9ecd3d commit ee226c3

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

backend/main/chapters/c08_nested_loops.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,11 @@ class nested_list_nested_loop_example(VerbatimStep):
11491149
"""
11501150
Excellent! You now understand nested subscripting very well.
11511151
1152-
Let us combine our knowledge of nested loops with nested lists.
1153-
Start with the following nested list (resembling pages in a book):
1152+
You can use a nested loop to iterate over each element and sub-element of a nested list.
1153+
For example, consider this nested list.
1154+
You can imagine that it represents a book, where each sublist is a page and each string within
1155+
is a line in that page.
1156+
It could also represent a library, where each list is a book, and each string is a page.
11541157
11551158
__copyable__
11561159
book = [
@@ -1168,11 +1171,11 @@ class nested_list_nested_loop_example(VerbatimStep):
11681171
]
11691172
]
11701173
1171-
Now add the following nested loop. What will the output look like?
1174+
Click the button to copy the list into the editor, then type in the following nested loop.
11721175
11731176
for page in book:
1174-
for sentence in page:
1175-
print(sentence)
1177+
for line in page:
1178+
print(line)
11761179
print('---')
11771180
11781181
"""
@@ -1195,8 +1198,8 @@ def program(self):
11951198
]
11961199
]
11971200
for page in book:
1198-
for sentence in page:
1199-
print(sentence)
1201+
for line in page:
1202+
print(line)
12001203
print('---')
12011204

12021205
predicted_output_choices = ["""\
@@ -1218,30 +1221,11 @@ def program(self):
12181221
---
12191222
The car turned the corner.
12201223
Kelly twirled in circles,
1221-
---
1222-
she opened the door.
1223-
Aaron made a picture.
1224-
---
1225-
""", """\
1226-
The cat stretched.
1227-
Jacob stood on his tiptoes.
1228-
---
1229-
The car turned the corner.
1230-
Kelly twirled in circles,
12311224
she opened the door.
12321225
---
12331226
Aaron made a picture.
12341227
---
12351228
""", """\
1236-
The cat stretched.
1237-
Jacob stood on his tiptoes.
1238-
The car turned the corner.
1239-
---
1240-
Kelly twirled in circles,
1241-
she opened the door.
1242-
Aaron made a picture.
1243-
---
1244-
""", """\
12451229
The cat stretched. Jacob stood on his tiptoes.
12461230
---
12471231
The car turned the corner. Kelly twirled in circles, she opened the door.
@@ -1254,7 +1238,7 @@ class nested_list_loop_python_tutor(VerbatimStep):
12541238
"""
12551239
Run the program again in Python Tutor.
12561240
Examine what `book` looks like, and what `book[0]`, `book[1]` and `book[2]` are.
1257-
Look at how `page` and `sentence` variables advance.
1241+
Look at how `page` and `line` variables advance.
12581242
"""
12591243

12601244
program_in_text = False
@@ -1277,18 +1261,18 @@ def program(self):
12771261
]
12781262
]
12791263
for page in book:
1280-
for sentence in page:
1281-
print(sentence)
1264+
for line in page:
1265+
print(line)
12821266
print('---')
12831267

12841268
final_text = """
12851269
We can still use all the list methods and functions we learned before.
1286-
For example we can add a new sentence to the last page of `book` with `append`,
1270+
For example we can add a new line to the last page of `book` with `append`,
12871271
to come after `"Aaron made a picture."`:
12881272
12891273
book[2].append("The car pulled into the garage.")
12901274
1291-
After all, the sublist `book[2]` is still a list for all intents and purposes!
1275+
After all, the sublist `book[2]` is still a list like any other!
12921276
12931277
On the next page we will exercise more with nested lists.
12941278
"""

backend/main/tests/test_transcript.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12836,8 +12836,8 @@
1283612836
" ]",
1283712837
"]",
1283812838
"for page in book:",
12839-
" for sentence in page:",
12840-
" print(sentence)",
12839+
" for line in page:",
12840+
" print(line)",
1284112841
" print('---')"
1284212842
],
1284312843
"response": {
@@ -12847,9 +12847,7 @@
1284712847
"answer": "The cat stretched.\nJacob stood on his tiptoes.\n---\nThe car turned the corner.\nKelly twirled in circles,\nshe opened the door.\n---\nAaron made a picture.\n---",
1284812848
"choices": [
1284912849
"The cat stretched.\n---\nJacob stood on his tiptoes.\n---\nThe car turned the corner.\n---\nKelly twirled in circles,\n---\nshe opened the door.\n---\nAaron made a picture.\n---",
12850-
"The cat stretched.\nJacob stood on his tiptoes.\n---\nThe car turned the corner.\nKelly twirled in circles,\n---\nshe opened the door.\nAaron made a picture.\n---",
1285112850
"The cat stretched.\nJacob stood on his tiptoes.\n---\nThe car turned the corner.\nKelly twirled in circles,\nshe opened the door.\n---\nAaron made a picture.\n---",
12852-
"The cat stretched.\nJacob stood on his tiptoes.\nThe car turned the corner.\n---\nKelly twirled in circles,\nshe opened the door.\nAaron made a picture.\n---",
1285312851
"The cat stretched. Jacob stood on his tiptoes.\n---\nThe car turned the corner. Kelly twirled in circles, she opened the door.\n---\nAaron made a picture.\n---",
1285412852
"Error"
1285512853
]
@@ -12953,8 +12951,8 @@
1295312951
" ]",
1295412952
"]",
1295512953
"for page in book:",
12956-
" for sentence in page:",
12957-
" print(sentence)",
12954+
" for line in page:",
12955+
" print(line)",
1295812956
" print('---')"
1295912957
],
1296012958
"response": {

0 commit comments

Comments
 (0)