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

Commit5f94183

Browse files
committed
Add change_parameter_name exercise
1 parentb507488 commit5f94183

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

‎backend/main/chapters/c08_functions.py‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ class change_function_name(VerbatimStep):
8181

8282
program_in_text=False
8383

84+
hints="""
85+
You have to change the name in the function definition header, but that's not all.
86+
If you just change the name in the function definition header, what happens?
87+
You get an error. Look at the message. What is it telling you? Where does the error come from?
88+
If your function is called `say_hello`, what does `greet("Alice")` mean?
89+
You need to change exactly 3 lines of the program.
90+
In each line you change, change exactly one word.
91+
Don't touch the body of the function. It should still have `print("How are you?")`.
92+
You should still call the function twice after defining it, with arguments `"Alice"` and `"Bob"`.
93+
"""
94+
8495
defprogram(self):
8596
defsay_hello(name):
8697
print("Hello "+name+"!")
@@ -89,14 +100,45 @@ def say_hello(name):
89100
say_hello("Alice")
90101
say_hello("Bob")
91102

103+
classchange_parameter_name(VerbatimStep):
104+
"""
105+
Good! Now do a similar exercise: change the name of the parameter from `name` to `person_name`.
106+
"""
107+
108+
program_in_text=False
109+
110+
hints="""
111+
You have to change the parameter name in the function definition header, but that's not all.
112+
If you just change the parameter name in the function definition header, what happens?
113+
You get an error. Look at the message. What is it telling you? Where does the error come from?
114+
If the parameter is called `person_name`, what does `print("Hello " + name + "!")` mean?
115+
You need to change exactly 2 lines of the program.
116+
In each line you change, change exactly one word.
117+
Don't touch the part after the function definition, i.e. `say_hello("Alice")` and `say_hello("Bob")`.
118+
You should still have two statements in the function body, including `print("How are you?")`.
119+
"""
120+
121+
defprogram(self):
122+
defsay_hello(person_name):
123+
print("Hello "+person_name+"!")
124+
print("How are you?")
125+
126+
say_hello("Alice")
127+
say_hello("Bob")
128+
92129
classprint_twice_exercise(ExerciseStep):
93-
""""
94-
Here's an exercise: write a function called `print_twice` which accepts one argument `x` and prints that argument twice.
130+
"""
131+
Well done!
132+
133+
Now write your own function called `print_twice` which accepts one argument `x` and prints that argument twice
134+
on two lines.
95135
96136
For example, `print_twice("Hello")` should output:
97137
98138
Hello
99139
Hello
140+
141+
You can test your function by calling it after the function definition, but it's not required.
100142
"""
101143
function_name="print_twice"
102144

@@ -110,7 +152,6 @@ class print_twice_exercise(ExerciseStep):
110152
Use the parameter inside the function body.
111153
Make sure the body is indented.
112154
The body needs two statements or a very simple loop.
113-
You can test your function by calling it after the function definition, but it's not required.
114155
Make sure that you don't call your function inside the function body. Check your indentation.
115156
"""
116157

‎backend/main/test_transcript.json‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15082,6 +15082,61 @@
1508215082
},
1508315083
"step":"change_function_name"
1508415084
},
15085+
{
15086+
"page":"Defining Functions",
15087+
"program": [
15088+
"def say_hello(person_name):",
15089+
" print(\"Hello\" + person_name +\"!\")",
15090+
" print(\"How are you?\")",
15091+
"",
15092+
"say_hello(\"Alice\")",
15093+
"say_hello(\"Bob\")"
15094+
],
15095+
"response": {
15096+
"birdseye_url":null,
15097+
"message":"",
15098+
"passed":true,
15099+
"result": [
15100+
{
15101+
"color":"white",
15102+
"text":"Hello Alice!"
15103+
},
15104+
{
15105+
"color":"white",
15106+
"text":"\n"
15107+
},
15108+
{
15109+
"color":"white",
15110+
"text":"How are you?"
15111+
},
15112+
{
15113+
"color":"white",
15114+
"text":"\n"
15115+
},
15116+
{
15117+
"color":"white",
15118+
"text":"Hello Bob!"
15119+
},
15120+
{
15121+
"color":"white",
15122+
"text":"\n"
15123+
},
15124+
{
15125+
"color":"white",
15126+
"text":"How are you?"
15127+
},
15128+
{
15129+
"color":"white",
15130+
"text":"\n"
15131+
},
15132+
{
15133+
"color":"white",
15134+
"text":">>>"
15135+
}
15136+
]
15137+
},
15138+
"step":"change_parameter_name"
15139+
},
1508515140
{
1508615141
"page":"Defining Functions",
1508715142
"program": [

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp