Skip to content
Open
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
120 changes: 117 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,127 @@
"\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": 1,
"id": "dc452e7f-6618-49ee-8850-4b782c1b0dc4",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter the quantity of tshirt you want: 2\n",
"enter the quantity of mug you want: 3\n",
"enter the quantity of hat you want: 4\n",
"enter the quantity of book you want: 5\n",
"enter the quantity of keychain you want: 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'tshirt': 2, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 6}\n"
]
}
],
"source": [
"#1 Look at your code from the lab data structures, and improve repeated code with loops.\n",
"products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products : \n",
" quantity = int(input(f\"enter the quantity of {product} you want: \"))\n",
" inventory [product] = quantity\n",
"\n",
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "452f5e79-3d80-4d44-8867-776d81962758",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter the name of the product you want to order: mug\n",
"do you want to order another product? (yes/no): yes\n",
"enter the name of the product you want to order: book\n",
"do you want to order another product? (yes/no): yes\n",
"enter the name of the product you want to order: hat\n",
"do you want to order another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'book', 'mug'}\n"
]
}
],
"source": [
"#2 Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n",
"#a. Prompt the user to enter the name of a product that a customer wants to order.\n",
"#b. Add the product name to the \"customer_orders\" set.\n",
"#c. Ask the user if they want to add another product (yes/no).\n",
"#d. Continue the loop until the user does not want to add another product.\n",
"\n",
"customer_orders = set()\n",
"product_order = input(\"enter the name of the product you want to order: \")\n",
"customer_orders.add(product_order)\n",
"x = input(\"do you want to order another product? (yes/no): \")\n",
"while x == \"yes\": \n",
" product_order = input(\"enter the name of the product you want to order: \")\n",
" x = input(\"do you want to order another product? (yes/no): \")\n",
" customer_orders.add(product_order)\n",
" \n",
"print (customer_orders )"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0836d325-0a02-4b0d-8773-99519b74a67e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'tshirt': 2, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 6}\n"
]
}
],
"source": [
"# Instead of updating the inventory by subtracting 1 from the quantity of each product, \n",
"#only do it for the products that were ordered (those in \"customer_orders\"). \n",
"for order in customer_orders : \n",
" if order in inventory.keys(): \n",
" inventory [order]= inventory [order] - 1\n",
" \n",
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "989a6fe0-c58d-4f16-91ce-0329c52445c0",
"metadata": {},
"outputs": [],
"source": []
}
],
"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 +169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down