Skip to content

Commit 21fdbbe

Browse files
committed
Chapter 29 - before John but published
1 parent f0735a1 commit 21fdbbe

File tree

3 files changed

+303
-4
lines changed

3 files changed

+303
-4
lines changed
193 KB
Binary file not shown.
+299
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 29. Usernames and passwords\n",
8+
"\n",
9+
"## Function outputs and modular design\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 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"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"---\n",
31+
"## 1. Creating a username"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"- Write a function that creates a username composed of the initial of the first name and the last name:"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"def create_username (first_name, last_name):\n",
48+
" \"\"\"Create a lowercase username made of initial of first name and last name\n",
49+
" \n",
50+
" Parameters\n",
51+
" ----------\n",
52+
" first_name: string \n",
53+
" First name of a person\n",
54+
" last_name: string\n",
55+
" Last name of a person\n",
56+
" \n",
57+
" Returns \n",
58+
" -------\n",
59+
" username: string\n",
60+
" Created username \n",
61+
" \"\"\"\n",
62+
" \n",
63+
" # concatenate initial of first name and last name\n",
64+
" username = first_name[0] + last_name\n",
65+
" # make sure username is lowercase\n",
66+
" username = username.lower()\n",
67+
" \n",
68+
" # return username\n",
69+
" return username"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"- Test the function for two customers:"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"username_1 = create_username (\"Julia\", \"Smith\") \n",
86+
"print (username_1)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"username_2 = create_username (\"Mohammed\", \"Seid\")\n",
96+
"print (username_2)"
97+
]
98+
},
99+
{
100+
"cell_type": "markdown",
101+
"metadata": {},
102+
"source": [
103+
"---\n",
104+
"## 2. Creating a password\n",
105+
"- Write a function that creates a password composed of four random integers:"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"import random\n",
115+
"\n",
116+
"def create_password():\n",
117+
" \"\"\"Create a password composed of four random integers \n",
118+
" \n",
119+
" Returns \n",
120+
" -------\n",
121+
" password: string\n",
122+
" Created password \n",
123+
" \"\"\"\n",
124+
" \n",
125+
" # create a random number with four digits\n",
126+
" password = str(random.randint(1000,9999))\n",
127+
" \n",
128+
" # return password\n",
129+
" return password"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"- Test the function for two customers:"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"password_1 = create_password()\n",
146+
"print (password_1)"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"password_2 = create_password()\n",
156+
"print (password_2)"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {},
162+
"source": [
163+
"---\n",
164+
"## 3. Creating a database\n",
165+
"\n",
166+
"- Write a function that, given a list of lists of customers, creates and returns a database (i.e., a dictionary) of usernames and passwords. The function also returns the number of customers in the\n",
167+
"database:"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": null,
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"def create_database (customers): \n",
177+
" \"\"\"Creates a database as a dictionary with usernames as keys and passwords as values \n",
178+
" \n",
179+
" Parameters\n",
180+
" ----------\n",
181+
" customers : list of lists\n",
182+
" Each sublist contains first name and last name of a customer\n",
183+
" \n",
184+
" Returns \n",
185+
" -------\n",
186+
" db : dictionary\n",
187+
" Created database (shorted as db)\n",
188+
" n_customers : int\n",
189+
" Number of customers in the database\n",
190+
" \"\"\"\n",
191+
" \n",
192+
" # initialize dictionary (i.e. database)\n",
193+
" db = {}\n",
194+
" \n",
195+
" # for each customer\n",
196+
" for customer in customers:\n",
197+
"\n",
198+
" # create username\n",
199+
" username = create_username (customer[0], customer[1])\n",
200+
"\n",
201+
" # create password\n",
202+
" password = create_password() \n",
203+
" \n",
204+
" # add username and password to db\n",
205+
" db[username] = password\n",
206+
" \n",
207+
" # compute number of customers \n",
208+
" n_customers = len(db)\n",
209+
"\n",
210+
" # return dictionary and its length\n",
211+
" return db, n_customers"
212+
]
213+
},
214+
{
215+
"cell_type": "markdown",
216+
"metadata": {},
217+
"source": [
218+
"- Given the following list of customers:"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": null,
224+
"metadata": {},
225+
"outputs": [],
226+
"source": [
227+
"customers = [[\"Maria\", \"Lopez\"], [\"Julia\", \"Smith\"], [\"Mohammed\", \"Seid\"]]"
228+
]
229+
},
230+
{
231+
"cell_type": "markdown",
232+
"metadata": {},
233+
"source": [
234+
"- Create the database using two different syntaxes:"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": null,
240+
"metadata": {},
241+
"outputs": [],
242+
"source": [
243+
"# create the database - separate returns\n",
244+
"database, number_customers = create_database(customers)\n",
245+
"\n",
246+
"# print the outputs\n",
247+
"print (\"Database:\", database)\n",
248+
"print (\"Number of customers:\", number_customers) "
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": null,
254+
"metadata": {},
255+
"outputs": [],
256+
"source": [
257+
"# create the database - single return\n",
258+
"outputs = create_database(customers)\n",
259+
"print (\"Output tuple:\", outputs)\n",
260+
"\n",
261+
"# get and print the database\n",
262+
"database = outputs[0]\n",
263+
"print(\"Database:\", database)\n",
264+
" \n",
265+
"# get and print the number of customers\n",
266+
"number_customers = outputs[1]\n",
267+
"print (\"Number of customers:\", number_customers) "
268+
]
269+
}
270+
],
271+
"metadata": {
272+
"kernelspec": {
273+
"display_name": "Python 3 (ipykernel)",
274+
"language": "python",
275+
"name": "python3"
276+
},
277+
"language_info": {
278+
"codemirror_mode": {
279+
"name": "ipython",
280+
"version": 3
281+
},
282+
"file_extension": ".py",
283+
"mimetype": "text/x-python",
284+
"name": "python",
285+
"nbconvert_exporter": "python",
286+
"pygments_lexer": "ipython3",
287+
"version": "3.9.6"
288+
},
289+
"widgets": {
290+
"application/vnd.jupyter.widget-state+json": {
291+
"state": {},
292+
"version_major": 2,
293+
"version_minor": 0
294+
}
295+
}
296+
},
297+
"nbformat": 4,
298+
"nbformat_minor": 4
299+
}

index.html

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

246246
</div>
@@ -348,7 +348,7 @@ <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-
<li>Username and password<br> <i>Function outputs</i></li>
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>
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>
354354
<li>Jupyter notebooks and modules<br> <i>Working with Jupyter Notebook and an IDE</i></li>

0 commit comments

Comments
 (0)