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

Commite8bdfd7

Browse files
committed
Add multiple parameters stuff
1 parent5f94183 commite8bdfd7

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

‎backend/main/chapters/c08_functions.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,62 @@ def solution(self, x: str):
165165
123:"123\n123\n",
166166
}
167167

168+
classprint_many(VerbatimStep):
169+
"""
170+
Functions can have many parameters. Here's an example:
171+
172+
__program_indented__
173+
174+
Note the commas used to separate parameters in the function definition and arguments in the function call,
175+
and the correspondence between the definition and the call:
176+
177+
def print_many(thing, n):
178+
^ ^
179+
| |
180+
print_many("Hello", 3)
181+
182+
So calling `print_many("Hello", 3)` is like running:
183+
184+
thing = "Hello"
185+
n = 3
186+
for _ in range(n):
187+
print(thing)
188+
189+
Try it now.
190+
"""
191+
192+
defprogram(self):
193+
defprint_many(thing,n):
194+
for_inrange(n):
195+
print(thing)
196+
197+
print_many("Hello",3)
198+
199+
classswap_parameters(VerbatimStep):
200+
"""
201+
Now for another simple exercise. Swap around the parameters in the function definition header so that it says:
202+
203+
def print_many(n, thing):
204+
205+
If you do this and nothing else, you will get an error. Fix the rest of the program so that it behaves like before.
206+
*Don't change the body of the function*.
207+
"""
208+
program_in_text=False
209+
210+
hints="""
211+
The only change to the function definition should be the swapping of parameters as instructed, nothing else.
212+
You need to fix the call to `print_many`.
213+
If the function is defined as `def print_many(n, thing)`, what does `print_many("Hello", 3)` mean?
214+
We still want `thing = "Hello"` and `n = 3`.
215+
"""
216+
217+
defprogram(self):
218+
defprint_many(n,thing):
219+
for_inrange(n):
220+
print(thing)
221+
222+
print_many(3,"Hello")
223+
168224
final_text="""
169225
Well done! You've reached the end for now. More coming soon!
170226
"""

‎backend/main/test_transcript.json‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15156,5 +15156,97 @@
1515615156
]
1515715157
},
1515815158
"step":"print_twice_exercise"
15159+
},
15160+
{
15161+
"page":"Defining Functions",
15162+
"program": [
15163+
"def print_many(thing, n):",
15164+
" for _ in range(n):",
15165+
" print(thing)",
15166+
"",
15167+
"print_many(\"Hello\", 3)"
15168+
],
15169+
"response": {
15170+
"birdseye_url":null,
15171+
"message":"",
15172+
"passed":true,
15173+
"result": [
15174+
{
15175+
"color":"white",
15176+
"text":"Hello"
15177+
},
15178+
{
15179+
"color":"white",
15180+
"text":"\n"
15181+
},
15182+
{
15183+
"color":"white",
15184+
"text":"Hello"
15185+
},
15186+
{
15187+
"color":"white",
15188+
"text":"\n"
15189+
},
15190+
{
15191+
"color":"white",
15192+
"text":"Hello"
15193+
},
15194+
{
15195+
"color":"white",
15196+
"text":"\n"
15197+
},
15198+
{
15199+
"color":"white",
15200+
"text":">>>"
15201+
}
15202+
]
15203+
},
15204+
"step":"print_many"
15205+
},
15206+
{
15207+
"page":"Defining Functions",
15208+
"program": [
15209+
"def print_many(n, thing):",
15210+
" for _ in range(n):",
15211+
" print(thing)",
15212+
"",
15213+
"print_many(3,\"Hello\")"
15214+
],
15215+
"response": {
15216+
"birdseye_url":null,
15217+
"message":"",
15218+
"passed":true,
15219+
"result": [
15220+
{
15221+
"color":"white",
15222+
"text":"Hello"
15223+
},
15224+
{
15225+
"color":"white",
15226+
"text":"\n"
15227+
},
15228+
{
15229+
"color":"white",
15230+
"text":"Hello"
15231+
},
15232+
{
15233+
"color":"white",
15234+
"text":"\n"
15235+
},
15236+
{
15237+
"color":"white",
15238+
"text":"Hello"
15239+
},
15240+
{
15241+
"color":"white",
15242+
"text":"\n"
15243+
},
15244+
{
15245+
"color":"white",
15246+
"text":">>>"
15247+
}
15248+
]
15249+
},
15250+
"step":"swap_parameters"
1515915251
}
1516015252
]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp