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

Commite3a9502

Browse files
committed
Add step print_twice_call_print_many
1 parente8bdfd7 commite3a9502

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

‎backend/main/chapters/c08_functions.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,26 @@ def print_many(n, thing):
221221

222222
print_many(3,"Hello")
223223

224+
classprint_twice_call_print_many(VerbatimStep):
225+
"""
226+
The body of a function can contain anything, including function calls. In fact we've already done that by calling
227+
print. But calling one of our own functions is no different, so our functions can call each other!
228+
229+
For example, we can implement `print_twice` using `print_many`:
230+
231+
__program_indented__
232+
"""
233+
234+
defprogram(self):
235+
defprint_many(n,thing):
236+
for_inrange(n):
237+
print(thing)
238+
239+
defprint_twice(x):
240+
print_many(2,x)
241+
242+
print_twice("Hello")
243+
224244
final_text="""
225-
Well done! You've reached the end for now. More coming soon!
245+
Well done! You've reached the endof the coursefor now. More coming soon!
226246
"""

‎backend/main/test_transcript.json‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15248,5 +15248,46 @@
1524815248
]
1524915249
},
1525015250
"step":"swap_parameters"
15251+
},
15252+
{
15253+
"page":"Defining Functions",
15254+
"program": [
15255+
"def print_many(n, thing):",
15256+
" for _ in range(n):",
15257+
" print(thing)",
15258+
"",
15259+
"def print_twice(x):",
15260+
" print_many(2, x)",
15261+
"",
15262+
"print_twice(\"Hello\")"
15263+
],
15264+
"response": {
15265+
"birdseye_url":null,
15266+
"message":"",
15267+
"passed":true,
15268+
"result": [
15269+
{
15270+
"color":"white",
15271+
"text":"Hello"
15272+
},
15273+
{
15274+
"color":"white",
15275+
"text":"\n"
15276+
},
15277+
{
15278+
"color":"white",
15279+
"text":"Hello"
15280+
},
15281+
{
15282+
"color":"white",
15283+
"text":"\n"
15284+
},
15285+
{
15286+
"color":"white",
15287+
"text":">>>"
15288+
}
15289+
]
15290+
},
15291+
"step":"print_twice_call_print_many"
1525115292
}
1525215293
]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp