Skip to content

Commit 5e3997a

Browse files
committed
lab solved
1 parent 392006e commit 5e3997a

1 file changed

Lines changed: 269 additions & 3 deletions

File tree

lab-python-functions.ipynb

Lines changed: 269 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,279 @@
4343
"\n",
4444
"\n"
4545
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 1,
50+
"id": "40e1d6ad-fa61-43d2-84a5-b68844b7c778",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 180,
60+
"id": "327bde1e-5ffe-4d17-93dc-207741327ea0",
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"# Define a function named initialize_inventory that takes products as a parameter. \n",
65+
"#Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
66+
"def initialize_inventory(products):\n",
67+
" inventory = {}\n",
68+
" \"\"\"Shows the number of each item\"\"\"\n",
69+
" for item in products:\n",
70+
" number_of_items = int(input(\"Number of \" + item + \": \")) \n",
71+
" inventory[item] = number_of_items\n",
72+
" print (inventory)\n",
73+
" return inventory "
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 168,
79+
"id": "35135cb7-d43b-4537-b5ea-f0ba9d2a3bf6",
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stdin",
84+
"output_type": "stream",
85+
"text": [
86+
"Number of t-shirt: 10\n",
87+
"Number of mug: 10\n",
88+
"Number of hat: 10\n",
89+
"Number of book: 10\n",
90+
"Number of keychain: 10\n"
91+
]
92+
},
93+
{
94+
"name": "stdout",
95+
"output_type": "stream",
96+
"text": [
97+
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
98+
]
99+
},
100+
{
101+
"data": {
102+
"text/plain": [
103+
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}"
104+
]
105+
},
106+
"execution_count": 168,
107+
"metadata": {},
108+
"output_type": "execute_result"
109+
}
110+
],
111+
"source": [
112+
"initialize_inventory(products)"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 172,
118+
"id": "7a2437ca-3681-49e0-8898-fe6f4067b1fd",
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"# Define a function named get_customer_orders that takes no parameters. \n",
123+
"# Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
124+
"# The function should return the customer_orders set.\n",
125+
"\n",
126+
"def get_customer_orders ():\n",
127+
" customer_orders = set ()\n",
128+
" answer = \"yes\"\n",
129+
" while answer == \"yes\":\n",
130+
" product = input(\"Choose a product: \")\n",
131+
" customer_orders.add(product)\n",
132+
" answer = input(\"Do you want something else? (yes/no): \").lower()\n",
133+
" print (\"customer_ order\", customer_orders) "
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 173,
139+
"id": "d088eabd-adf4-4a04-a61e-fd3f4326ccd8",
140+
"metadata": {},
141+
"outputs": [
142+
{
143+
"name": "stdin",
144+
"output_type": "stream",
145+
"text": [
146+
"Choose a product: book\n",
147+
"Do you want something else? (yes/no): yes\n",
148+
"Choose a product: hat\n",
149+
"Do you want something else? (yes/no): no\n"
150+
]
151+
},
152+
{
153+
"name": "stdout",
154+
"output_type": "stream",
155+
"text": [
156+
"customer_ order {'book', 'hat'}\n"
157+
]
158+
}
159+
],
160+
"source": [
161+
"get_customer_orders ()"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 181,
167+
"id": "86be53b5-2c9a-4f21-bd05-cccb95e47b6a",
168+
"metadata": {},
169+
"outputs": [],
170+
"source": [
171+
"#3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
172+
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
173+
"\n",
174+
"\n",
175+
"def updated_inventory (customer_orders, inventory):\n",
176+
" new_inventory = {}\n",
177+
" for item in customer_orders:\n",
178+
" if item in inventory:\n",
179+
" inventory [item]-= 1\n",
180+
" return inventory \n"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 182,
186+
"id": "2c940a7e-9af8-4aa5-a9d3-86739c75f715",
187+
"metadata": {},
188+
"outputs": [
189+
{
190+
"data": {
191+
"text/plain": [
192+
"{'t-shirt': 1, 'mug': 0, 'hat': 0, 'book': 0, 'keychain': 1}"
193+
]
194+
},
195+
"execution_count": 182,
196+
"metadata": {},
197+
"output_type": "execute_result"
198+
}
199+
],
200+
"source": [
201+
" updated_inventory (customer_orders, inventory)"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 213,
207+
"id": "941ac3fb-0f28-480c-bf8b-795e30f987b6",
208+
"metadata": {},
209+
"outputs": [],
210+
"source": [
211+
"# 4 Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
212+
"#Inside the function, implement the code for calculating the order statistics \n",
213+
"#(total products ordered, and percentage of unique products ordered). \n",
214+
"#The function should return these values.\n",
215+
"\n",
216+
"def calculate_order_statistics(customer_orders, products):\n",
217+
" total = len(customer_orders)\n",
218+
" percentage = len(customer_orders) * 100 / len(products)\n",
219+
" return total, percentage"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 212,
225+
"id": "ad5111f1-f623-4dfb-ae9d-8015a30da88d",
226+
"metadata": {},
227+
"outputs": [
228+
{
229+
"data": {
230+
"text/plain": [
231+
"(4, 80.0)"
232+
]
233+
},
234+
"execution_count": 212,
235+
"metadata": {},
236+
"output_type": "execute_result"
237+
}
238+
],
239+
"source": [
240+
"calculate_order_statistics (customer_orders, products)"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 219,
246+
"id": "8caaeaef-81c9-4dc4-98a0-dba2558dd29f",
247+
"metadata": {},
248+
"outputs": [
249+
{
250+
"name": "stdout",
251+
"output_type": "stream",
252+
"text": [
253+
"4\n",
254+
"80.0\n"
255+
]
256+
}
257+
],
258+
"source": [
259+
"# Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. \n",
260+
"# Inside the function, implement the code for printing the order statistics.\n",
261+
"\n",
262+
"def print_order_statistics(order_statistics):\n",
263+
" total, percentage = order_statistics\n",
264+
" print(total)\n",
265+
" print(percentage)\n",
266+
"\n",
267+
"stats = calculate_order_statistics(customer_orders, products)\n",
268+
"print(stats[0]) # total\n",
269+
"print(stats[1]) # percentage \n",
270+
" \n"
271+
]
272+
},
273+
{
274+
"cell_type": "code",
275+
"execution_count": 222,
276+
"id": "f1d59425-db7c-4b75-bf22-ab8ee0403f06",
277+
"metadata": {},
278+
"outputs": [],
279+
"source": [
280+
"# 6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
281+
"#Inside the function, implement the code for printing the updated inventory.\n",
282+
"\n",
283+
"def print_updated_inventory (inventory):\n",
284+
" print(inventory)"
285+
]
286+
},
287+
{
288+
"cell_type": "code",
289+
"execution_count": 221,
290+
"id": "0156e6a4-0983-4837-a047-b7554944e5ef",
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"name": "stdout",
295+
"output_type": "stream",
296+
"text": [
297+
"{'t-shirt': 1, 'mug': 0, 'hat': 0, 'book': 0, 'keychain': 1}\n"
298+
]
299+
}
300+
],
301+
"source": [
302+
"print_updated_inventory (inventory)"
303+
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": null,
308+
"id": "0242022c-ead2-4c8c-94d5-ff986479ae10",
309+
"metadata": {},
310+
"outputs": [],
311+
"source": []
46312
}
47313
],
48314
"metadata": {
49315
"kernelspec": {
50-
"display_name": "Python 3 (ipykernel)",
316+
"display_name": "Python [conda env:base] *",
51317
"language": "python",
52-
"name": "python3"
318+
"name": "conda-base-py"
53319
},
54320
"language_info": {
55321
"codemirror_mode": {
@@ -61,7 +327,7 @@
61327
"name": "python",
62328
"nbconvert_exporter": "python",
63329
"pygments_lexer": "ipython3",
64-
"version": "3.9.13"
330+
"version": "3.13.5"
65331
}
66332
},
67333
"nbformat": 4,

0 commit comments

Comments
 (0)