You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -269,37 +269,51 @@ class name_triangle(VerbatimStep):
269
269
This is very useful in a loop. Try out this program:
270
270
271
271
__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.
274
272
"""
275
273
276
274
predicted_output_choices= [
275
+
"-\n"
277
276
"W\n"
277
+
"-\n"
278
278
"o\n"
279
+
"-\n"
279
280
"r\n"
281
+
"-\n"
280
282
"l\n"
283
+
"-\n"
281
284
"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",
298
312
]
299
313
300
314
defprogram(self):
301
315
name='World'
302
-
line=''
316
+
line='-'
303
317
forcharinname:
304
318
line=line+char
305
319
print(line)
@@ -308,25 +322,25 @@ class name_triangle_missing_last_line(VerbatimStep):
308
322
"""
309
323
Take your time to make sure you understand this program fully. It's doing something like this:
310
324
311
-
line = ''
325
+
line = '-'
312
326
313
327
char = 'W'
314
328
line = line + char
315
-
= '' + 'W'
316
-
= 'W'
317
-
print('W')
329
+
= '-' + 'W'
330
+
= '-W'
331
+
print('-W')
318
332
319
333
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')
324
338
325
339
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')
330
344
331
345
...
332
346
@@ -336,31 +350,112 @@ class name_triangle_missing_last_line(VerbatimStep):
336
350
"""
337
351
338
352
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"
353
367
]
354
368
355
369
defprogram(self):
356
370
name='World'
357
-
line=''
371
+
line='-'
358
372
forcharinname:
359
373
print(line)
360
374
line=line+char
361
375
362
-
final_text="""
376
+
classempty_string(VerbatimStep):
377
+
"""
363
378
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
+
classname_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
+
defsolution(self, name: str):
433
+
line=''
434
+
forcharinname:
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.
0 commit comments