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
107 changes: 104 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,114 @@
"\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": "45a299c1-9b82-4762-b4b6-1c6f2e3fe21c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of 't-shirt' available in the inventory 10\n",
"Enter the number of 'mug' available in the inventory 10\n",
"Enter the number of 'hat' available in the inventory 10\n",
"Enter the number of 'book' available in the inventory 10\n",
"Enter the number of 'keychain' available in the inventory 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory dictionary is: {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
]
}
],
"source": [
"# 1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"for product in products:\n",
" qty = int(input(f\"Enter the number of '{product}' available in the inventory\"))\n",
" inventory[product] = qty\n",
"print(\"Inventory dictionary is:\", inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "b48096aa-a80e-48b5-88c4-e4a8d0b2bfea",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please chose a product from list:'t-shirt', 'mug', 'hat', 'book', 'keychain': hat\n",
"Do you need another product? please answer YES/NO: yes\n",
"Please chose a product from list:'t-shirt', 'mug', 'hat', 'book', 'keychain': book\n",
"Do you need another product? please answer YES/NO: yes\n",
"Please chose a product from list:'t-shirt', 'mug', 'hat', 'book', 'keychain': keychain\n",
"Do you need another product? please answer YES/NO: n\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'keychain', 'hat'}\n",
"Inventory dictionary is: {'t-shirt': 10, 'mug': 9, 'hat': 10, 'book': 9, 'keychain': 10}\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",
"customer_orders = set()\n",
"product = input(\"Please chose a product from list:'t-shirt', 'mug', 'hat', 'book', 'keychain': \")\n",
"customer_orders.add(product)\n",
"user_answer = input(\"Do you need another product? please answer YES/NO: \")\n",
"while user_answer.lower() == \"yes\":\n",
" product = input(\"Please chose a product from list:'t-shirt', 'mug', 'hat', 'book', 'keychain': \")\n",
" customer_orders.add(product)\n",
" user_answer = input(\"Do you need another product? please answer YES/NO: \")\n",
" #user_answer = input(\"Do you need another product? please answer YES/NO: \")\n",
"\n",
"print(customer_orders)\n",
"print(\"Inventory dictionary is:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "fd574709-d8c1-44db-b54f-bd7f160b2c25",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory dictionary is: {'t-shirt': 10, 'mug': 9, 'hat': 9, 'book': 8, 'keychain': 9}\n"
]
}
],
"source": [
"# 3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it\n",
"# for the products that were ordered (those in \"customer_orders\").\n",
"for item in customer_orders:\n",
" inventory[item] = inventory[item] -1\n",
"print(\"Inventory dictionary is:\", inventory)"
]
}
],
"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 +156,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down