Skip to content

finished lab #531

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
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
168 changes: 166 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,175 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"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']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Create an empty dictionary called `inventory`.\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"quantity = int(input('How many t-shirts are available: '))\n",
"inventory['t-shirt'] = quantity\n",
"quantity = int(input('How many mug are available: '))\n",
"inventory['mug'] = quantity\n",
"quantity = int(input('How many hat are available: '))\n",
"inventory['hat'] = quantity\n",
"quantity = int(input('How many book are available: '))\n",
"inventory['book'] = quantity\n",
"quantity = int(input('How many keychain are available: '))\n",
"inventory['keychain'] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"# Upgraded version from above\n",
"for product in products:\n",
" quantity = int(input(f'How many {product}s are available: '))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Create an empty set called `customer_orders`.\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n",
"order = input('Please say which of the following items you would like: t-shirt, mug, hat, book, keychain')\n",
"customer_orders.add(order)\n",
"order = input('Please say which of the following items you would like: t-shirt, mug, hat, book, keychain')\n",
"customer_orders.add(order)\n",
"order = input('Please say which of the following items you would like: t-shirt, mug, hat, book, keychain')\n",
"customer_orders.add(order)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'hat', 'mug'}\n"
]
}
],
"source": [
"# Print the products in the `customer_orders` set.\n",
"print(customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered : 3\n",
"Percentage of products ordered : 60.0%\n"
]
}
],
"source": [
"# Calculate and print the following order statistics:\n",
" # - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"products_ordered = len(customer_orders)\n",
"print(f'Total products ordered : {products_ordered}')\n",
" # - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"percent = (len(customer_orders)/len(products))*100\n",
"print(f'Percentage of products ordered : {percent}%')\n",
" # Store these statistics in a tuple called `order_status`.\n",
"order_status = (products_ordered, percent)\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': -3, 'hat': -2, 'book': -3, 'keychain': 5}\n"
]
}
],
"source": [
"# Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"print(inventory) # this was the inventory before customer purchase\n",
"prod_1 = list(customer_orders)[0]\n",
"inventory[prod_1]-=1\n",
"prod_2 = list(customer_orders)[1]\n",
"inventory[prod_2]-=1\n",
"prod_3 = list(customer_orders)[2]\n",
"inventory[prod_3]-=1"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': -4, 'hat': -3, 'book': -4, 'keychain': 5}\n"
]
}
],
"source": [
"# Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +232,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down