Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit60d62d6

Browse files
committed
Merge branch 'spanish-final' into translation-preview
2 parentse781d2f +9c2ab32 commit60d62d6

File tree

47 files changed

+2547
-880
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2547
-880
lines changed

‎core/chapters/c05_if_statements.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def program(self):
210210

211211

212212
classUnderstandingProgramsWithSnoop(Page):
213+
title="Understanding Programs With `snoop`"
214+
213215
classprint_tail(print_tail_base):
214216
"""
215217
Run this program:
@@ -228,15 +230,15 @@ class print_tail(print_tail_base):
228230
classprint_tail_snoop(print_tail_base):
229231
"""
230232
Programs are starting to get complicated!
231-
It's time to introduce a new tool to help you understand programs. Click the'Snoop' button to run the same program while also showing what's happening.
233+
It's time to introduce a new tool to help you understand programs. Click the`snoop` button to run the same program while also showing what's happening.
232234
"""
233235

234236
program_in_text=False
235237
expected_code_source="snoop"
236238

237239
classprint_first_character(ExerciseStep):
238240
"""
239-
Tada! Scroll to the top of the terminal and let's walk through what snoop is showing you.
241+
Tada! Scroll to the top of the terminal and let's walk through what`snoop` is showing you.
240242
It starts out very straightforward:
241243
242244
1 | sentence = 'Hello World'
@@ -248,9 +250,9 @@ class print_first_character(ExerciseStep):
248250
The first lines are simply showing you the lines of the program that the computer ran.
249251
On the left is the line number as seen in the editor.
250252
251-
Running `for char in sentence:` assigns a value to the variable `char`, so snoop shows you that value.
253+
Running `for char in sentence:` assigns a value to the variable `char`, so`snoop` shows you that value.
252254
Lines starting with `......` indicate a new variable or a change in the value of an existing variable.
253-
Such lines will not be shown when they're redundant, which is why the snoop output doesn't start like this:
255+
Such lines will not be shown when they're redundant, which is why the`snoop` output doesn't start like this:
254256
255257
1 | sentence = 'Hello World'
256258
...... sentence = 'Hello World'

‎core/chapters/c06_lists.py‎

Lines changed: 255 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def generate_inputs(cls):
379379
break
380380
381381
This is just as correct but skips unnecessary iterations and checks once it finds the element.
382-
You can use snoop to see the difference.
382+
You can use`snoop` to see the difference.
383383
"""
384384

385385

@@ -423,30 +423,12 @@ def check(self):
423423
classintroducing_len_and_range(VerbatimStep):
424424
"""
425425
There you go. `words[4]` and beyond don't exist, so trying that will give you an error.
426-
427-
By the way, you can get the number of elements in a list (commonly called the *length*) using `len(words)`.
428-
That means that the last valid index of the list is `len(words) - 1`, so the last element is `words[len(words) - 1]`. Try these for yourself.
429-
430-
So in general, the valid indices are:
431-
432-
[0, 1, 2, ..., len(words) - 2, len(words) - 1]
433-
434-
There's a handy built in function called `range` which gives you these values. Try it by running this code:
426+
That first program is a bit repetitive. Let's improve it with a list and a loop!
435427
436428
__program_indented__
437429
"""
438430

439-
defprogram(self):
440-
foriinrange(10):
441-
print(i)
442-
443-
classrange_len(VerbatimStep):
444-
"""
445-
`range(n)` is similar to the list `[0, 1, 2, ..., n - 2, n - 1]`.
446-
This gives us an alternative way to loop over a list:
447-
448-
__program_indented__
449-
"""
431+
auto_translate_program=False
450432

451433
predicted_output_choices= ["""\
452434
This
@@ -494,21 +476,272 @@ class range_len(VerbatimStep):
494476
1
495477
2
496478
3
479+
"""
480+
]
481+
482+
defprogram(self):
483+
words= ['This','is','a','list']
484+
indices= [0,1,2,3]
485+
486+
forindexinindices:
487+
print(index)
488+
print(words[index])
489+
490+
classrange_len(VerbatimStep):
491+
"""
492+
That's a bit better, but writing out `[0, 1, 2, ...]` isn't great, especially if it gets long.
493+
There's a handy function `range` to do that part for you. Replace `[0, 1, 2, 3]` with `range(4)`,
494+
i.e. `indices = range(4)`.
495+
"""
496+
497+
program_in_text=False
498+
499+
auto_translate_program=False
500+
501+
defprogram(self):
502+
words= ['This','is','a','list']
503+
indices=range(4)
504+
505+
forindexinindices:
506+
print(index)
507+
print(words[index])
508+
509+
classprinting_the_range(VerbatimStep):
510+
"""
511+
As you can see, the result is the same. Try this:
512+
513+
__copyable__
514+
__program_indented__
515+
"""
516+
517+
predicted_output_choices= ["""\
518+
0
519+
1
520+
2
521+
3
522+
""","""\
523+
1
524+
2
525+
3
526+
4
527+
""","""\
528+
[0]
529+
[1]
530+
[2]
531+
[3]
532+
""","""\
533+
[1]
534+
[2]
535+
[3]
536+
[4]
537+
""","""\
538+
This
539+
is
540+
a
541+
list
497542
""",
498543
]
499544

545+
defprogram(self):
546+
indices=range(4)
547+
548+
print(indices[0])
549+
print(indices[1])
550+
print(indices[2])
551+
print(indices[3])
552+
553+
classindices_out_of_bounds(VerbatimStep):
554+
"""
555+
Now try `__program__` in the shell.
556+
"""
557+
558+
predicted_output_choices= ["0","1","2","3","4"]
559+
560+
correct_output="Error"
561+
562+
expected_code_source="shell"
563+
564+
program="indices[4]"
565+
566+
classrange_almost_the_same_as_list(VerbatimStep):
567+
"""
568+
`range(4)` is the same thing as `[0, 1, 2, 3]` ... almost. Try `__program__` in the shell.
569+
"""
570+
571+
expected_code_source="shell"
572+
573+
program="range(4)"
574+
575+
classrange_versus_list(VerbatimStep):
576+
"""
577+
That's probably a bit surprising. If you're curious, the `0` represents the start of the range.
578+
`0` is the default start, so `range(4)` is equal to `range(0, 4)`.
579+
`4` is the end of the range, but the end is always excluded, so the last value is `3`.
580+
If you're confused now, don't worry about it.
581+
582+
There's a good reason for why `range(4)` is not actually a list - it makes programs faster and more efficient.
583+
It's not worth explaining that more right now.
584+
585+
But you can easily convert it to a list: try `__program__` in the shell.
586+
"""
587+
predicted_output_choices= [
588+
"range(4)",
589+
"range(0, 4)",
590+
"list(range(4))",
591+
"list(range(0, 4))",
592+
"range(0, 1, 2, 3)",
593+
"(0, 1, 2, 3)",
594+
"[0, 1, 2, 3]"
595+
]
596+
597+
expected_code_source="shell"
598+
599+
program="list(range(4))"
600+
601+
classusing_len_first_time(VerbatimStep):
602+
"""
603+
That's just a demonstration to let you see a range in a more familiar form.
604+
You should almost never actually do that.
605+
606+
If you're feeling overwhelmed, don't worry! All you need to know is that `range(n)`
607+
is very similar to the list:
608+
609+
[0, 1, 2, ..., n - 2, n - 1]
610+
611+
By the way, you can get the number of elements in a list (commonly called the *length*) using the `len` function.
612+
Try it by running this code:
613+
614+
__copyable__
615+
__program_indented__
616+
"""
617+
500618
auto_translate_program=False
501619

620+
predicted_output_choices= ["0","1","2","3","4","5"]
621+
502622
defprogram(self):
503623
words= ['This','is','a','list']
624+
print(len(words))
625+
626+
classprint_last_element(ExerciseStep):
627+
"""
628+
Exercise: for any non-empty list `words`, print the last element. For example, if
629+
630+
__no_auto_translate__
631+
words = ['This', 'is', 'a', 'list']
632+
633+
your program should print `list`.
634+
"""
635+
636+
hints="""
637+
To access the last element of the list, you'll need the index of the last position.
638+
If the list has 2 elements, the first element is at index 0, so the last element is at index 1.
639+
Likewise, if the list had 3 elements, the last element would be at index 2.
640+
Do you see a pattern between those numbers? How can you express it?
641+
Can you come up with a general solution that works for any length?
642+
"""
643+
644+
defsolution(self,words:List[str]):
645+
print(words[len(words)-1])
646+
647+
tests= [
648+
(["Python"],"Python"),
649+
(['Hello','world'],"world"),
650+
(['futurecoder','is','cool!'],"cool!"),
651+
(['This','is','a','list'],"list")
652+
]
653+
654+
classprint_indices_and_words(ExerciseStep):
655+
"""
656+
So in general, the valid indices are:
657+
658+
[0, 1, 2, ..., len(words) - 2, len(words) - 1]
659+
660+
Now we can fix the program from earlier to work with any list. Fill in the `...`:
504661
662+
__copyable__
663+
__no_auto_translate__
664+
words = ['This', 'is', 'a', 'list']
665+
666+
for index in ...:
667+
print(index)
668+
print(words[index])
669+
670+
For the given example value of `words` it should print:
671+
672+
0
673+
This
674+
1
675+
is
676+
2
677+
a
678+
3
679+
list
680+
"""
681+
682+
hints="""
683+
Remember that earlier we used `range(4)`.
684+
This time, it should work for any list. What if the list has 5 elements, or 10?
685+
Combine the two functions you learned!
686+
"""
687+
688+
defsolution(self,words:List[str]):
505689
forindexinrange(len(words)):
506690
print(index)
507691
print(words[index])
508692

693+
tests= [
694+
(['Python'],"""\
695+
0
696+
Python
697+
"""),
698+
(['Hello','world'],"""\
699+
0
700+
Hello
701+
1
702+
world
703+
"""),
704+
(['futurecoder','is','cool!'],"""\
705+
0
706+
futurecoder
707+
1
708+
is
709+
2
710+
cool!
711+
"""),
712+
(['This','is','a','list'],"""\
713+
0
714+
This
715+
1
716+
is
717+
2
718+
a
719+
3
720+
list
721+
"""),
722+
]
723+
724+
final_text="""
725+
If you're still not quite comfortable with `range` and/or `len`, practice and experiment with it for a bit.
726+
Here are some simple exercises you can try on your own if you want.
727+
728+
- Print the numbers from `1` to `100` inclusive.
729+
- Print your name 100 times.
730+
- Print each word in a list `words` except for the last one.
731+
- Print each word in `words` in reverse order, i.e. print the last word, then the second last word, etc.
732+
- Revisit the bonus problem at the end of the [Introducing Lists page](#IntroducingLists),
733+
whether or not you completed it. It's now much easier with `range` and `len`!
734+
735+
When you're ready, continue to the next page for something a bit more challenging.
736+
"""
737+
738+
739+
classGettingElementsAtPositionExercises(Page):
740+
title="Exercises with `range()` and `len()`"
741+
509742
classindex_exercise(ExerciseStep):
510743
"""
511-
Let's get some exercise!Given a list `things` and a value `to_find`,
744+
Given a list `things` and a value `to_find`,
512745
print the first index of `to_find` in the list, i.e. the lowest number `i` such that
513746
`things[i]` is `to_find`. For example, for
514747

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp