Skip to content

Commit e25241e

Browse files
committed
Separate learning about the empty string from adding strings in a loop
1 parent 39d4a9e commit e25241e

File tree

3 files changed

+324
-82
lines changed

3 files changed

+324
-82
lines changed

core/chapters/c04_for_loops.py

Lines changed: 142 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -269,37 +269,51 @@ class name_triangle(VerbatimStep):
269269
This is very useful in a loop. Try out this program:
270270
271271
__program_indented__
272-
273-
By the way, `''` is called the *empty string* - a string containing no characters. Adding it to another string just gives you the other string unchanged, in the same way that `0 + 5` is just `5`. Don't confuse the empty string with `' '`, which is a non-empty string containing one character: a space.
274272
"""
275273

276274
predicted_output_choices = [
275+
"-\n"
277276
"W\n"
277+
"-\n"
278278
"o\n"
279+
"-\n"
279280
"r\n"
281+
"-\n"
280282
"l\n"
283+
"-\n"
281284
"d",
282-
"World",
283-
"W\n"
284-
"Wo\n"
285-
"Wor\n"
286-
"Worl\n"
287-
"World\n",
288-
"World\n"
289-
"Worl\n"
290-
"Wor\n"
291-
"Wo\n"
292-
"W\n",
293-
"World\n"
294-
"World\n"
295-
"World\n"
296-
"World\n"
297-
"World\n",
285+
"-W\n"
286+
"-o\n"
287+
"-r\n"
288+
"-l\n"
289+
"-d",
290+
"-World",
291+
"-W-o-r-l-d",
292+
"-W\n"
293+
"-Wo\n"
294+
"-Wor\n"
295+
"-Worl\n"
296+
"-World\n",
297+
"-World\n"
298+
"-Worl\n"
299+
"-Wor\n"
300+
"-Wo\n"
301+
"-W\n",
302+
"-World\n"
303+
"-World\n"
304+
"-World\n"
305+
"-World\n"
306+
"-World\n",
307+
"-World\n"
308+
"--World\n"
309+
"---World\n"
310+
"----World\n"
311+
"-----World\n",
298312
]
299313

300314
def program(self):
301315
name = 'World'
302-
line = ''
316+
line = '-'
303317
for char in name:
304318
line = line + char
305319
print(line)
@@ -308,25 +322,25 @@ class name_triangle_missing_last_line(VerbatimStep):
308322
"""
309323
Take your time to make sure you understand this program fully. It's doing something like this:
310324
311-
line = ''
325+
line = '-'
312326
313327
char = 'W'
314328
line = line + char
315-
= '' + 'W'
316-
= 'W'
317-
print('W')
329+
= '-' + 'W'
330+
= '-W'
331+
print('-W')
318332
319333
char = 'o'
320-
line = line + char
321-
= 'W' + 'o'
322-
= 'Wo'
323-
print('Wo')
334+
line = line + char
335+
= '-W' + 'o'
336+
= '-Wo'
337+
print('-Wo')
324338
325339
char = 'r'
326-
line = line + char
327-
= 'Wo' + 'r'
328-
= 'Wor'
329-
print('Wor')
340+
line = line + char
341+
= '-Wo' + 'r'
342+
= '-Wor'
343+
print('-Wor')
330344
331345
...
332346
@@ -336,31 +350,112 @@ class name_triangle_missing_last_line(VerbatimStep):
336350
"""
337351

338352
predicted_output_choices = [
339-
"W\n"
340-
"Wo\n"
341-
"Wor\n"
342-
"Worl\n"
343-
"World\n",
344-
"Wo\n"
345-
"Wor\n"
346-
"Worl\n"
347-
"World\n",
348-
"\n"
349-
"W\n"
350-
"Wo\n"
351-
"Wor\n"
352-
"Worl\n"
353+
"-W\n"
354+
"-Wo\n"
355+
"-Wor\n"
356+
"-Worl\n"
357+
"-World\n",
358+
"-Wo\n"
359+
"-Wor\n"
360+
"-Worl\n"
361+
"-World\n",
362+
"-\n"
363+
"-W\n"
364+
"-Wo\n"
365+
"-Wor\n"
366+
"-Worl\n"
353367
]
354368

355369
def program(self):
356370
name = 'World'
357-
line = ''
371+
line = '-'
358372
for char in name:
359373
print(line)
360374
line = line + char
361375

362-
final_text = """
376+
class empty_string(VerbatimStep):
377+
"""
363378
The last character in `name` only gets added to `line` at the end of the loop, after `print(line)` has already run for the last time. So that character and the full `name` never get printed at the bottom of the triangle.
379+
380+
Let's get rid of those `-` characters in the output. You might already be able to guess how.
381+
382+
An *empty string* is a string containing no characters at all.
383+
It's written as just a pair of quotes surrounding nothing: `''`.
384+
It's like the zero of strings.
385+
Adding it to another string just gives you the other string unchanged,
386+
in the same way that `0 + 5` is just `5`.
387+
388+
Try this in the shell:
389+
390+
__program_indented__
391+
"""
392+
393+
expected_code_source = "shell"
394+
395+
program = "'' + '' + ''"
396+
397+
predicted_output_choices = [
398+
"''",
399+
"' '",
400+
"' '",
401+
"' '",
402+
"'' + '' + ''",
403+
"''''''",
404+
"'' '' ''",
405+
"' '' '' '",
406+
"++",
407+
]
408+
409+
class name_triangle_empty_string(ExerciseStep):
410+
"""
411+
Don't confuse the empty string with `' '`, which is a non-empty string containing one character: a space.
412+
413+
Now fix the original program to get rid of those lines in the output, so that
414+
for `name = 'World'` it prints:
415+
416+
__no_auto_translate__
417+
W
418+
Wo
419+
Wor
420+
Worl
421+
World
422+
"""
423+
424+
hints = """
425+
First make sure you're not working from the broken version of the previous program on this page.
426+
That is, `line = line + char` should come before `print(line)`.
427+
Apart from that, you only need to make one ***tiny*** change.
428+
We want to get rid of the `-`. So just do that. Literally.
429+
Use an empty string!
430+
"""
431+
432+
def solution(self, name: str):
433+
line = ''
434+
for char in name:
435+
line = line + char
436+
print(line)
437+
438+
tests = {
439+
'World': """\
440+
W
441+
Wo
442+
Wor
443+
Worl
444+
World
445+
""",
446+
'Bob': """\
447+
B
448+
Bo
449+
Bob
450+
""",
451+
}
452+
453+
final_text = """
454+
Isn't that pretty?
455+
456+
The pattern of starting with something empty and building it up with a `for` loop is *very* common
457+
and you're going to get lots of practice with it. Some initial empty values are
458+
`''`, `0`, and `[]` - an empty list, which you'll see soon.
364459
"""
365460

366461

0 commit comments

Comments
 (0)