Skip to content

Commit 18a622a

Browse files
committed
Publish Ch. 28
1 parent fb6a046 commit 18a622a

File tree

3 files changed

+289
-4
lines changed

3 files changed

+289
-4
lines changed
160 KB
Binary file not shown.
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 28. Printing *Thank you* cards\n",
8+
"\n",
9+
"## Function inputs\n",
10+
"\n",
11+
"[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n",
12+
"Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n",
13+
"\n",
14+
"---"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## 1. Basic *Thank you* cards\n",
22+
"- You recently hosted a party, and you want to send *Thank you* cards to those who attended. Create\n",
23+
"a function that takes a first name as an argument and prints a *Thank you* message containing an\n",
24+
"attendee’s name (e.g., *Thank you Maria*): "
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"def print_thank_you(first_name): \n",
34+
" \"\"\"Prints a string containing \"Thank you\" and a first name\n",
35+
" \n",
36+
" Parameters\n",
37+
" ----------\n",
38+
" first_name: string\n",
39+
" First name of a person\n",
40+
" \"\"\"\n",
41+
" \n",
42+
" print (\"Thank you\", first_name)"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"- Print two *Thank you* cards: "
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"print_thank_you(\"Maria\")"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"print_thank_you(\"Xiao\")"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": [
74+
"---\n",
75+
"## 2. Formal *Thank you* cards\n",
76+
"- After a second thought, you decide that it is more appropriate to print formal Thank you cards. Mod-\n",
77+
"ify the previous function to take three arguments—prefix, first name, and last name—and to print\n",
78+
"a thank you message containing them (e.g., *Thank you Mrs. Maria Lopez*): "
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"def print_thank_you(prefix, first_name, last_name): \n",
88+
" \"\"\"Prints a string containing \"Thank you\" and the inputs\n",
89+
" \n",
90+
" Parameters\n",
91+
" ----------\n",
92+
" prefix: string\n",
93+
" Usually Ms, Mrs, Mr\n",
94+
" first_name: string\n",
95+
" First name of a person\n",
96+
" last_name: string\n",
97+
" Last name of a person\n",
98+
" \"\"\"\n",
99+
"\n",
100+
" print (\"Thank you\", prefix, first_name, last_name)"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"- Print two formal *Thank you* cards: "
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"print_thank_you(\"Mrs\", \"Maria\", \"Lopez\")"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": null,
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"print_thank_you(\"Mr\", \"Xiao\", \"Li\")"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"---\n",
133+
"## 3. Last name missing! \n",
134+
"- You are very happy with the *Thank you* cards, but you suddenly realize that some participants did\n",
135+
"not provide their last names! Adapt the function so that the last name has an empty string as a\n",
136+
"default value:"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"def print_thank_you(prefix, first_name, last_name=\"\"): \n",
146+
" \"\"\"Prints a string containing \"Thank you\" and the inputs\n",
147+
" \n",
148+
" Parameters\n",
149+
" ----------\n",
150+
" prefix: string\n",
151+
" Usually Ms, Mrs, Mr\n",
152+
" first_name: string\n",
153+
" First name of a person\n",
154+
" last_name: string\n",
155+
" Last name of a person. The default is an empty string\n",
156+
" \"\"\"\n",
157+
" \n",
158+
" print (\"Thank you\", prefix, first_name, last_name)"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"- Print two *Thank you* cards, one with a last name and one without a last name:"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": null,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": [
174+
"print_thank_you(\"Mrs\", \"Maria\", \"Lopez\")"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"print_thank_you(\"Mr\", \"Xiao\")"
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"metadata": {},
189+
"source": [
190+
"---\n",
191+
"## 4. Prefix and/or first name missing! \n",
192+
"- Finally, you realize that prefix and/or first name are also missing for some guests. Modify the function accordingly:"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": [
201+
"def print_thank_you(prefix=\"\", first_name=\"\", last_name=\"\"): \n",
202+
" \"\"\"Prints each input and a string containing \"Thank you\" and the inputs\n",
203+
" \n",
204+
" Parameters\n",
205+
" ----------\n",
206+
" prefix: string\n",
207+
" Usually Ms, Mrs, Mr. The default is an empty string\n",
208+
" first_name: string\n",
209+
" First name of a person. The default is an empty string\n",
210+
" last_name: string\n",
211+
" Last name of a person. The default is an empty string\n",
212+
" \"\"\"\n",
213+
" \n",
214+
" print (\"Prefix:\", prefix)\n",
215+
" print (\"First name:\", first_name)\n",
216+
" print (\"Last name:\", last_name)\n",
217+
" print (\"Thank you\", prefix, first_name, last_name)"
218+
]
219+
},
220+
{
221+
"cell_type": "markdown",
222+
"metadata": {},
223+
"source": [
224+
"- Print a *Thank you* card where the *first name* is missing:"
225+
]
226+
},
227+
{
228+
"cell_type": "code",
229+
"execution_count": null,
230+
"metadata": {
231+
"tags": []
232+
},
233+
"outputs": [],
234+
"source": [
235+
"print_thank_you(prefix=\"Mrs\", last_name=\"Lopez\")"
236+
]
237+
},
238+
{
239+
"cell_type": "markdown",
240+
"metadata": {},
241+
"source": [
242+
"- Print a *Thank you* card where the *prefix* is missing:"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": null,
248+
"metadata": {
249+
"tags": []
250+
},
251+
"outputs": [],
252+
"source": [
253+
"print_thank_you(first_name=\"Xiao\", last_name=\"Li\")"
254+
]
255+
}
256+
],
257+
"metadata": {
258+
"kernelspec": {
259+
"display_name": "Python 3 (ipykernel)",
260+
"language": "python",
261+
"name": "python3"
262+
},
263+
"language_info": {
264+
"codemirror_mode": {
265+
"name": "ipython",
266+
"version": 3
267+
},
268+
"file_extension": ".py",
269+
"mimetype": "text/x-python",
270+
"name": "python",
271+
"nbconvert_exporter": "python",
272+
"pygments_lexer": "ipython3",
273+
"version": "3.9.6"
274+
},
275+
"widgets": {
276+
"application/vnd.jupyter.widget-state+json": {
277+
"state": {},
278+
"version_major": 2,
279+
"version_minor": 0
280+
}
281+
}
282+
},
283+
"nbformat": 4,
284+
"nbformat_minor": 4
285+
}

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ <h2>About the Book</h2>
235235
<div class="col-lg-12">
236236

237237
<div class="progress">
238-
<span class="skill">BOOK COMPLETION <i class="val">73%</i></span>
238+
<span class="skill">BOOK COMPLETION <i class="val">75%</i></span>
239239
<div class="progress-bar-wrap">
240-
<div class="progress-bar" role="progressbar" aria-valuenow="73" aria-valuemin="0" aria-valuemax="100"></div>
240+
<div class="progress-bar" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100"></div>
241241
</div>
242242
</div>
243-
<center>Next upload: <b>Chapter 28</b> on <b>September 9</b> </center>
243+
<center>Next upload: <b>Chapter 29</b> on <b>November 18</b> </center>
244244
</div>
245245

246246
</div>
@@ -347,7 +347,7 @@ <h4>PART 7: Dictionaries and overview of strings</h4>
347347
<div class="resume-item pb-0">
348348
<h4>PART 8: Functions </h4>
349349
<ol start="28">
350-
<li>Stationary for a party<br> <i>Function inputs</i></li>
350+
<a href="assets/notebooks/28_function_inputs.ipynb" onclick="download" ></a><li>Printing Thank you cards<br> <i>Function inputs</i></li>
351351
<li>Username and password<br> <i>Function outputs</i></li>
352352
<li>People's age and odd numbers<br> <i>Type and value errors, and return</i></li>
353353
<li>Factorials<br> <i>Recursive functions</i></li>

0 commit comments

Comments
 (0)