Skip to content

Solutions lab day-2 part-1 #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": []
},
"source": [
"# Lab | Flow Control"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
"\n",
"In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n",
"\n",
"You did so without using flow control. Let's go a step further and improve this code.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
"\n",
"2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n",
" \n",
" a. Prompt the user to enter the name of a product that a customer wants to order.\n",
" \n",
" b. Add the product name to the \"customer_orders\" set.\n",
" \n",
" c. Ask the user if they want to add another product (yes/no).\n",
" \n",
" d. Continue the loop until the user does not want to add another product.\n",
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "0e74c4ff-9a04-4aa6-ad40-4f7260ccabaf",
"metadata": {},
"outputs": [],
"source": [
"# Creating a list\n",
"products = [\"t-shirt\", \"mug\",\"hat\", \"book\", \"keychain\"]\n",
"# Creating an empty dictionary\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 142,
"id": "f7a5bb0e-2df7-4940-a5a2-a85e19cddd54",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter key: t-shirt\n",
"Enter value: 4\n",
"Enter key: mug\n",
"Enter value: 3\n",
"Enter key: hat\n",
"Enter value: 2\n",
"Enter key: book\n",
"Enter value: 4\n",
"Enter key: keychain\n",
"Enter value: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 3, 'hat': 2, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"# Input the quanitiy of each product and show the content of the inventory\n",
"inventory = {input(\"Enter key: \"): int(input(\"Enter value: \")) for product in products}\n",
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"id": "b3cfef14-d740-4e82-9a90-53604fd55545",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): mug\n",
"Do you want to add more products? (yes or no): yes\n",
"Enter a product (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): hat\n",
"Do you want to add more products? (yes or no): yes\n",
"Enter a product (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): book\n",
"Do you want to add more products? (yes or no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your trust. Customer orders are: {'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"# Step 2\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" value = input(f\"Enter a product (choose from {products}): \")\n",
" customer_orders.add(value)\n",
"\n",
" while True:\n",
" more_orders = input(\"Do you want to add more products? (yes or no): \")\n",
" if more_orders == \"yes\" or more_orders == \"no\":\n",
" break\n",
" else:\n",
" print(f\"Please type 'yes' or 'no'.\")\n",
"\n",
" if more_orders == \"no\":\n",
" break\n",
"\n",
"print(f\"Thank you for your trust. Customer orders are:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 146,
"id": "7bebae5b-aa85-437c-ba5b-f24fbb321af0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated mug: 2 left\n",
"Updated hat: 1 left\n",
"Updated book: 3 left\n",
"\n",
"Full inventory after updates:\n",
"t-shirt: 4 left\n",
"mug: 2 left\n",
"hat: 1 left\n",
"book: 3 left\n",
"keychain: 5 left\n"
]
}
],
"source": [
"for key in inventory:\n",
" if key in customer_orders:\n",
" inventory[key] = inventory[key] - 1\n",
" print (f\"Updated {key}: {inventory[key]} left\")\n",
"print(\"\\nFull inventory after updates:\")\n",
"for key, value in inventory.items():\n",
" print(f\"{key}: {value} left\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
136 changes: 133 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,143 @@
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "0e74c4ff-9a04-4aa6-ad40-4f7260ccabaf",
"metadata": {},
"outputs": [],
"source": [
"# Creating a list\n",
"products = [\"t-shirt\", \"mug\",\"hat\", \"book\", \"keychain\"]\n",
"# Creating an empty dictionary\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 142,
"id": "f7a5bb0e-2df7-4940-a5a2-a85e19cddd54",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter key: t-shirt\n",
"Enter value: 4\n",
"Enter key: mug\n",
"Enter value: 3\n",
"Enter key: hat\n",
"Enter value: 2\n",
"Enter key: book\n",
"Enter value: 4\n",
"Enter key: keychain\n",
"Enter value: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 3, 'hat': 2, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"# Input the quanitiy of each product and show the content of the inventory\n",
"inventory = {input(\"Enter key: \"): int(input(\"Enter value: \")) for product in products}\n",
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"id": "b3cfef14-d740-4e82-9a90-53604fd55545",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): mug\n",
"Do you want to add more products? (yes or no): yes\n",
"Enter a product (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): hat\n",
"Do you want to add more products? (yes or no): yes\n",
"Enter a product (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): book\n",
"Do you want to add more products? (yes or no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your trust. Customer orders are: {'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"# Step 2\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" value = input(f\"Enter a product (choose from {products}): \")\n",
" customer_orders.add(value)\n",
"\n",
" while True:\n",
" more_orders = input(\"Do you want to add more products? (yes or no): \")\n",
" if more_orders == \"yes\" or more_orders == \"no\":\n",
" break\n",
" else:\n",
" print(f\"Please type 'yes' or 'no'.\")\n",
"\n",
" if more_orders == \"no\":\n",
" break\n",
"\n",
"print(f\"Thank you for your trust. Customer orders are:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 146,
"id": "7bebae5b-aa85-437c-ba5b-f24fbb321af0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated mug: 2 left\n",
"Updated hat: 1 left\n",
"Updated book: 3 left\n",
"\n",
"Full inventory after updates:\n",
"t-shirt: 4 left\n",
"mug: 2 left\n",
"hat: 1 left\n",
"book: 3 left\n",
"keychain: 5 left\n"
]
}
],
"source": [
"for key in inventory:\n",
" if key in customer_orders:\n",
" inventory[key] = inventory[key] - 1\n",
" print (f\"Updated {key}: {inventory[key]} left\")\n",
"print(\"\\nFull inventory after updates:\")\n",
"for key, value in inventory.items():\n",
" print(f\"{key}: {value} left\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -55,7 +185,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down