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
51 changes: 49 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,58 @@
"\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": null,
"id": "3fe45d80",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the inventory before customer purchase : {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n",
"This is the inventory after customer purchase : {'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"# Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"# Create an empty dictionary called `inventory`.\n",
"inventory = {}\n",
"\n",
"# Ask the user to input the quantity of each product available in the inventory using loops. Use the product names \n",
"# from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"for product in products:\n",
" inventory[product] = int(input(f'How many {product}s are available: '))\n",
"print(f'This is the inventory before customer purchase : {inventory}')\n",
"\n",
"# Create an empty set called `customer_orders`.\n",
"customer_orders = set()\n",
"\n",
"# Prompt the user to enter the name of a product that a customer wants to order and add the product name to the\n",
"# \"customer_orders\" set.\n",
"answer = 'yes'\n",
"while answer == 'yes':\n",
" customer_orders.add(input('Please say which of the following items you would like: t-shirt, mug, hat, book, keychain'))\n",
" answer = input('Would you like to add another product to your order? (yes/no)')\n",
" while answer != 'yes' and answer!= 'no':\n",
" print('That is an invalid input, please answer yes or no')\n",
" answer = input('Would you like to add another product to your order? (yes/no)')\n",
"\n",
"# Update the inventory by subtracting 1 from the products that were ordered (those in \"customer_orders\").\n",
"for order in customer_orders:\n",
" inventory[order]-=1\n",
"print(f'This is the inventory after customer purchase : {inventory}')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +102,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down