Skip to content
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
115 changes: 0 additions & 115 deletions README.md

This file was deleted.

63 changes: 0 additions & 63 deletions lab-python-flow-control.ipynb

This file was deleted.

161 changes: 161 additions & 0 deletions lab-python-flow_controls (day 2).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"# Lab | Flow Controls\n",
"\n",
"Exercise: Managing Customer Orders Optimized\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",
"Look at your code from the lab data structures, and improve repeated code with loops.\n",
"\n",
"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",
"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": 13,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"\n",
"for product in products:\n",
" quantity = int(input(f\"Quantity of product {product}: \"))\n",
" inventory[product] = quantity\n",
" \n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The product hat is not in the lis of ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Your porducts are {'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"\n",
"customer_orders = set()\n",
"\n",
"for product in products:\n",
" chosen_product = input(f\"What porduct do you want to choose?{products}\")\n",
" if chosen_product in products:\n",
" customer_orders.add(chosen_product)\n",
" question = input(\"Do you want another product? yes/no\")\n",
" if question == \"yes\":\n",
" continue\n",
" elif question == \"no\":\n",
" break\n",
" else:\n",
" print(f\"The product {question} is not in the lis of {products}\")\n",
" else:\n",
" print(f\"Your {chosen_product} porduct is no in the list of porducts. {products}\")\n",
" \n",
" \n",
" \n",
"print(f\"Your porducts are {customer_orders}\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}\n",
"t-shirt:5\n",
"mug:4\n",
"hat:4\n",
"book:4\n",
"keychain:5\n"
]
}
],
"source": [
"for product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
"print(inventory)\n",
"\n",
"for key, values in inventory.items():\n",
" print(f\"{key}:{values}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}