Skip to content

Commit 5651d7c

Browse files
committed
Publish Ch 30
1 parent 21fdbbe commit 5651d7c

File tree

4 files changed

+236
-9
lines changed

4 files changed

+236
-9
lines changed
38.2 KB
Binary file not shown.

assets/notebooks/29_function_outputs.ipynb

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# 29. Usernames and passwords\n",
7+
"# 29. Login database for an online store\n",
88
"\n",
99
"## Function outputs and modular design\n",
1010
"\n",
@@ -19,8 +19,7 @@
1919
"metadata": {},
2020
"source": [
2121
"- You are the owner of an online shop and need to securely store usernames and passwords of your\n",
22-
"customers. Create a database where usernames are composed of the initial of the customer’s first\n",
23-
"name followed by their last name, and passwords consist of a four-digit code"
22+
"customers. Create a database where usernames are composed of the initial of the customer’s first name followed by their last name (e.g., “jsmith”), and passwords consist of a four-digit code"
2423
]
2524
},
2625
{
@@ -45,7 +44,7 @@
4544
"outputs": [],
4645
"source": [
4746
"def create_username (first_name, last_name):\n",
48-
" \"\"\"Create a lowercase username made of initial of first name and last name\n",
47+
" \"\"\"Create a lowercase username made of initial of first name and full last name\n",
4948
" \n",
5049
" Parameters\n",
5150
" ----------\n",
+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 30. Free ticket at the museum\n",
8+
"\n",
9+
"## Input validation and outputs variations\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+
"- You work at a museum and have to update the online system to buy tickets. The update is that people who are 65 and older now qualify for a free ticket. Write a function that asks visitors to enter their prefix, last name, and age; checks the *types* and *values* of these inputs; and returns a message telling the visitor if they are eligible for a free ticket."
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def free_museum_ticket(prefix, last_name, age): \n",
31+
" \"\"\"Returns a message containing inputs and free ticket eligibility based on age\n",
32+
" E.g. Mrs. Holmes, you are eligible for a free museum ticket because you are 66\n",
33+
" \n",
34+
" Parameters\n",
35+
" ----------\n",
36+
" prefix : string\n",
37+
" Ms, Mrs, Mr\n",
38+
" last_name : string\n",
39+
" Last name of a visitor\n",
40+
" age : integer\n",
41+
" Age of a visitor\n",
42+
" \n",
43+
" Returns\n",
44+
" -------\n",
45+
" string \n",
46+
" Message containing visitor inputs and eligibility\n",
47+
" \"\"\"\n",
48+
" \n",
49+
" # --- checking parameter types ---\n",
50+
" \n",
51+
" # the type of prefix must be string\n",
52+
" if not isinstance (prefix, str): \n",
53+
" raise TypeError (\"prefix must be a string\")\n",
54+
" \n",
55+
" # the type of last_name must be string\n",
56+
" if not isinstance (last_name, str): \n",
57+
" raise TypeError (\"last_name must be a string\")\n",
58+
" \n",
59+
" # the type of age must be integer\n",
60+
" if not isinstance (age, int): \n",
61+
" raise TypeError (\"age must be an integer\") \n",
62+
"\n",
63+
" \n",
64+
" # --- checking parameter values ---\n",
65+
" \n",
66+
" # prefix must be Ms, Mrs, or Mr\n",
67+
" if prefix not in [\"Ms\", \"Mrs\", \"Mr\"]:\n",
68+
" raise ValueError (\"prefix must be Ms, Mrs, or Mr\")\n",
69+
" \n",
70+
" # last_name must contain only characters \n",
71+
" if not last_name.isalpha():\n",
72+
" raise ValueError (\"last_name must contain only letters\")\n",
73+
" \n",
74+
" # age has to be between 0 and 125\n",
75+
" if age < 0 or age > 125:\n",
76+
" raise ValueError (\"age must be between 0 and 125\")\n",
77+
"\n",
78+
" \n",
79+
" # --- returning output ---\n",
80+
"\n",
81+
" if age >= 65:\n",
82+
" return prefix + \". \" + last_name + \", you are eligible for a free museum ticket because you are \" + str(age)\n",
83+
" else:\n",
84+
" return prefix + \". \" + last_name + \", you are not eligible for a free museum ticket because you are \" + str(age) "
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"metadata": {},
90+
"source": [
91+
"---\n",
92+
"- Call the function checking the parameter *types*:"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": null,
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"# checking prefix type\n",
102+
"message = free_museum_ticket(1, \"Holmes\", 66)\n",
103+
"print (message)"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"# checking last_name type\n",
113+
"message = free_museum_ticket(\"Mrs\", 1.2, 66)\n",
114+
"print (message)"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": null,
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"# checking age type\n",
124+
"message = free_museum_ticket(\"Mrs\", \"Holmes\", \"Hi\") \n",
125+
"print (message)"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"---\n",
133+
"- Call the function checking parameter *values*:"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"# checking prefix value\n",
143+
"message = free_museum_ticket(\"Dr\", \"Holmes\", 66)\n",
144+
"print (message)"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"# checking last_name value\n",
154+
"message = free_museum_ticket(\"Mrs\", \"82\", 66)\n",
155+
"print (message)"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"# checking age value\n",
165+
"message = free_museum_ticket(\"Mrs\", \"Holmes\", 130)\n",
166+
"print (message)"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {},
172+
"source": [
173+
"---\n",
174+
"- Call the function testing the two possible returns:"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"# person is eligible\n",
184+
"message = free_museum_ticket(\"Mrs\", \"Holmes\", 66)\n",
185+
"print (message)"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"metadata": {},
192+
"outputs": [],
193+
"source": [
194+
"# person is not eligible\n",
195+
"message = free_museum_ticket(\"Mrs\", \"Choi\", 38)\n",
196+
"print (message)"
197+
]
198+
}
199+
],
200+
"metadata": {
201+
"kernelspec": {
202+
"display_name": "Python 3 (ipykernel)",
203+
"language": "python",
204+
"name": "python3"
205+
},
206+
"language_info": {
207+
"codemirror_mode": {
208+
"name": "ipython",
209+
"version": 3
210+
},
211+
"file_extension": ".py",
212+
"mimetype": "text/x-python",
213+
"name": "python",
214+
"nbconvert_exporter": "python",
215+
"pygments_lexer": "ipython3",
216+
"version": "3.9.6"
217+
},
218+
"widgets": {
219+
"application/vnd.jupyter.widget-state+json": {
220+
"state": {},
221+
"version_major": 2,
222+
"version_minor": 0
223+
}
224+
}
225+
},
226+
"nbformat": 4,
227+
"nbformat_minor": 4
228+
}

index.html

+5-5
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">78%</i></span>
238+
<span class="skill">BOOK COMPLETION <i class="val">80%</i></span>
239239
<div class="progress-bar-wrap">
240-
<div class="progress-bar" role="progressbar" aria-valuenow="78" aria-valuemin="0" aria-valuemax="100"></div>
240+
<div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100"></div>
241241
</div>
242242
</div>
243-
<center>Next upload: <b>Chapter 30</b> on <b>February 3</b> </center>
243+
<center>Next upload: <b>Chapter 31</b> on <b>March 3</b> </center>
244244
</div>
245245

246246
</div>
@@ -348,8 +348,8 @@ <h4>PART 7: Dictionaries and overview of strings</h4>
348348
<h4>PART 8: Functions </h4>
349349
<ol start="28">
350350
<a href="assets/notebooks/28_function_inputs.ipynb" onclick="download" ><li>Printing Thank you cards<br> <i>Function inputs</i></li></a>
351-
<a href="assets/notebooks/29_function_outputs.ipynb" onclick="download" ><li>Usernames and passwords<br> <i>Function outputs and modular design</i></li></a>
352-
<li>People's age and odd numbers<br> <i>Type and value errors, and return</i></li>
351+
<a href="assets/notebooks/29_function_outputs.ipynb" onclick="download" ><li>Login database for an online store<br> <i>Function outputs and modular design</i></li></a>
352+
<a href="assets/notebooks/30_function_io_extra.ipynb" onclick="download" ><li>Free ticket at the museum<br> <i>Input validation and output variations</i></li></a>
353353
<li>Factorials<br> <i>Recursive functions</i></li>
354354
<li>Jupyter notebooks and modules<br> <i>Working with Jupyter Notebook and an IDE</i></li>
355355
</ol>

0 commit comments

Comments
 (0)